【问题标题】:Active Directory reading user attributesActive Directory 读取用户属性
【发布时间】:2014-07-31 14:18:14
【问题描述】:

我正在尝试让我的 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())); 

            }
    }
}

我在 Ad Explorer 中使用了相同的数据,一切正常,我可以获得所需的数据。但是从我的系统不能。

【问题讨论】:

  • 顺便说一句。使用 System.DirectoryServices.AccountManagement,它工作正常,但当然受限于属性

标签: c# winforms visual-studio-2010 active-directory ldap


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2011-12-23
    • 2018-04-08
    • 1970-01-01
    • 2011-02-27
    • 2012-03-10
    • 1970-01-01
    相关资源
    最近更新 更多