【问题标题】:How do I get group description of a user from external active directory using LDAP如何使用 LDAP 从外部活动目录中获取用户的组描述
【发布时间】:2018-02-15 16:21:16
【问题描述】:

我需要获取所有组名及其描述(其中用户是成员以及那些没有用户的组)。与外部域的连接必须通过端口 389 和用户凭据的 LDAP。

现在我可以使用以下代码验证用户:

public string UserValidation(string username, string domain, string password, string url)
{        
var credentials = new NetworkCredential(username, password, domain);        
var serverId = new LdapDirectoryIdentifier(url);
LdapConnection connection = new LdapConnection(serverId, credentials);        
string result = "true";            
try            
{                
connection.Bind();            
}            
catch (Exception e)            
{                
result = e.ToString();            
}            
connection.Dispose();            
return result;        
}

link 有助于获取群组,但不适用于外部域。

【问题讨论】:

    标签: c# ldap


    【解决方案1】:

    添加这个命名空间

    使用 System.DirectoryServices;

    那就试试这段代码

    DirectoryEntry de = new DirectoryEntry(urLDAPdomain, username, passwaord,  AuthenticationTypes.Secure);
    
      DirectorySearcher ds = new DirectorySearcher(de);
       // in ds u will get all  users and groups
    

    【讨论】:

    • 但是在哪里提供域控制器的 ip,因为我正在从外部域进行查询
    【解决方案2】:

    我通过使用以下代码获得了用户所属的组描述:

    var path = String.Format("LDAP://{0}:{1}", DomainControllerIP, Port);
                    DirectoryEntry rootDE = new DirectoryEntry(path, strUserName, strPassword);
                    DirectorySearcher dSearcher = new DirectorySearcher(rootDE);
                    dSearcher.Filter = "(&(sAMAccountName=" + strUserName + ")(objectClass=User)(objectCategory=Person))";
                    SearchResult sResult = dSearcher.FindOne();
                    foreach (var grp in sResult.Properties["memberOf"])
                        {
                            string sGrpName = (Convert.ToString(grp).Remove(0, 3)).Split(',')[0];
                            DirectorySearcher gSearcher = new DirectorySearcher(rootDE);
                            gSearcher.Filter = "sAMAccountName=" + sGrpName;
                            SearchResult gResult = gSearcher.FindOne();
                            //Group Name in groupName
                            string groupName = gResult.Properties["name"][0].ToString();
                        }
    

    获取所有组的描述:

    dSearcher.Filter = "(&(objectCategory=group))";
    dSearcher.PropertiesToLoad.Add("name");
    dSearcher.PropertiesToLoad.Add("description");
    
    SearchResultCollection results = dSearcher.FindAll();
    
    foreach (SearchResult res in results)
    {
        String name = ((res.Properties["name"])[0]).ToString();
        string groupDescription = (res.Properties["description"])[0].ToString();
    }
    

    【讨论】:

      猜你喜欢
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-03
      • 2011-07-06
      相关资源
      最近更新 更多