Active Directory读取用户属性

本文关键字:属性 用户 读取 Directory Active | 更新日期: 2023-09-27 18:16:00

我正在尝试让我的winform系统使用活动目录中人员的用户名进行身份验证。我现在使用下面的代码。但是结果是空的!!

 private static string LDAP_Connection = "corp.mycompany.global";
 private static string LDAP_Path = "LDAP://OU=USERS,OU=BT,OU=EC,OU=tres,DC=corp,DC=company,DC=global";

 static DirectoryEntry createDirectoryEntry()
        {
            // create and return new LDAP connection with desired settings  
            DirectoryEntry ldapConnection = new DirectoryEntry(LDAP_Connection);
            ldapConnection.Path = LDAP_Path;
            ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
            return ldapConnection;
        } 

public static void RetreiveUserInfoAdvanced()
{
    try
    {
        // create LDAP connection object  
        DirectoryEntry myLdapConnection = createDirectoryEntry();
        // create search object which operates on LDAP connection object  
        // and set search object to only find the user specified  
        DirectorySearcher search = new DirectorySearcher(myLdapConnection);
        //search.Filter = "(mail  =" + _userlogin + ")";
        search.Filter = "mail  = a.ghew@mycompany.com";
        // create results objects from search object  
        //SearchResult result = search.FindOne();
        string[] requiredProperties = new string[] { "cn", "mail" };  
            foreach (String property in requiredProperties)   
               search.PropertiesToLoad.Add(property);  
            SearchResult result = search.FindOne();  
            if (result != null)  
            {  
               foreach (String property in requiredProperties)  
                  foreach (Object myCollection in result.Properties[property])   
                     Console.WriteLine(String.Format("{0,-20} : {1}", property, myCollection.ToString())); 
            }
    }
}

我使用广告资源管理器与相同的数据,一切都是找到一个工作良好,我可以达到所需的数据。

Active Directory读取用户属性

我没有您的AD环境,但我在类似的配置中做了以下操作:

DirectorySearcher search = new DirectorySearcher(myLdapConnection);
search.Filter = "(mail=a.ghew@mycompany.com)";
search.SearchScope = SearchScope.Subtree;

试一试?基本上删除过滤器表达式中的空白,并确保启用了遍历。