【问题标题】:How to retrieve SAMAccountName from Active Directory如何从 Active Directory 中检索 SAMAccountName
【发布时间】:2012-04-11 18:47:40
【问题描述】:

我实现了一个返回 Active Directory 用户列表的方法,我想像 Domain\Administrator 这样检索 SAMAccountName。

这是我使用的方法:

public Collection<software_user> GetUsersFromAD(String adConnectionString)
{
    var users = new Collection<software_user>();

    using (var directoryEntry = new DirectoryEntry(adConnectionString))
    {
        var directorySearcher = new DirectorySearcher(directoryEntry);
        directorySearcher.Filter = "(&(objectClass=user))";
        var propertiesToLoad = new[] 
        { 
           "SAMAccountName", 
           "displayName", 
           "givenName", 
           "sn", 
           "mail", 
           "userAccountControl", 
           "objectSid" 
        };
        directorySearcher.PropertiesToLoad.AddRange(propertiesToLoad);

        foreach (SearchResult searchEntry in directorySearcher.FindAll())
        {
            var userEntry = searchEntry.GetDirectoryEntry();
            var ldapUser = new software_user();
            ldapUser.User_name = NullHandler.GetString(userEntry.Properties["displayName"].Value);

            if (string.IsNullOrEmpty(ldapUser.User_name))
               continue;
            ldapUser.User_name = NullHandler.GetString(userEntry.Properties["SAMAccountName"].Value);
            ldapUser.email = NullHandler.GetString(userEntry.Properties["mail"].Value);
            ldapUser.user_shortname = NullHandler.GetString(userEntry.Properties["givenName"].Value);
            var userAccountControl = (int)userEntry.Properties["userAccountControl"].Value;
            //ldapUser.IsActive = (userAccountControl & UF_ACCOUNTDISABLE) != UF_ACCOUNTDISABLE;
            var sid = new SecurityIdentifier((byte[])userEntry.Properties["objectSid"][0], 0).Value;
            //ldapUser.SId = sid;
            users.Add(ldapUser);
         }
    }
    return users;
}

【问题讨论】:

    标签: c# active-directory directoryservices


    【解决方案1】:

    首先Domain\Administrator 不是 SAM 帐户名! SAM 帐户名称是唯一的(在整个域中)最多 20 个字符的名称 - 通常是您的“Windows 用户名”(例如 Administrator) - 但它确实包括域名。由domain\username 组成的值存储在任何位置的 Active Directory 中!


    如果您使用的是 .NET 3.5 及更高版本,则应查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在此处阅读所有相关信息:

    基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:

    // set up domain context
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    
    // find a user
    UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
    
    if(user != null)
    {
       // do something here....     
       string samAccountName = user.SamAccountName;
    }
    

    新的 S.DS.AM 让在 AD 中与用户和组一起玩变得非常容易!

    如果您想搜索整个用户组(或组或计算机),您可以使用PrincipalSearcher 和“query-by-example”主体进行搜索:

    // create your domain context
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    
    // define a "query-by-example" principal - here, we search for a UserPrincipal 
    // and with the last name (Surname) of "Miller"
    UserPrincipal qbeUser = new UserPrincipal(ctx);
    qbeUser.Surname = "Miller";
    
    // create your principal searcher passing in the QBE principal    
    PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
    
    // find all matches
    foreach(var found in srch.FindAll())
    {
        // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用对象的 SID 和 System.Security.Principal.SecurityIdentifier.Translate 命令将用户作为可分辨名称转换为 DOMAIN\SAMaccount 表单。

      public Collection<software_user> GetUsersFromAD(String adConnectionString)
          {
                  var users = new Collection<software_user>();
      
                  using (var directoryEntry = new DirectoryEntry(adConnectionString))
                  {
                          var directorySearcher = new DirectorySearcher(directoryEntry);
                          directorySearcher.Filter = "(&(objectClass=user))";
                          var propertiesToLoad = new[] 
                          { 
                               "SAMAccountName", 
                               "displayName", 
                               "givenName", 
                               "sn", 
                               "mail", 
                               "userAccountControl", 
                               "objectSid" 
                          };
                          directorySearcher.PropertiesToLoad.AddRange(propertiesToLoad);
      
                          foreach (SearchResult searchEntry in directorySearcher.FindAll())
                          {
                                  var userEntry = searchEntry.GetDirectoryEntry();
                                  var ldapUser = new software_user();
                                  ldapUser.User_name = NullHandler.GetString(userEntry.Properties["displayName"].Value);
      
                                  if (string.IsNullOrEmpty(ldapUser.User_name))
                                       continue;
                                  ldapUser.User_name = NullHandler.GetString(userEntry.Properties["SAMAccountName"].Value);
                                  ldapUser.email = NullHandler.GetString(userEntry.Properties["mail"].Value);
                                  ldapUser.user_shortname = NullHandler.GetString(userEntry.Properties["givenName"].Value);
                                  var userAccountControl = (int)userEntry.Properties["userAccountControl"].Value;
      
                                  //ldapUser.IsActive = (userAccountControl & UF_ACCOUNTDISABLE) != UF_ACCOUNTDISABLE;
                                  SecurityIdentifier sid = new SecurityIdentifier((byte[])userEntry.Properties["objectSid"][0], 0).Value;
          -->                     NTAccount account = (NTAccount) sid.Translate(typeof(NTAccount));
          -->                     ldapUser.User_name = account.ToString();
      
                                  //ldapUser.SId = sid;
                                  users.Add(ldapUser);
                           }
                  }
                  return users;
          }
      

      【讨论】:

      • 我收到“无法翻译部分或全部身份参考”。翻译错误。
      • 多域林?您应该确保您的广告连接到您的森林 GC。也可能是您的实体确实是一个孤儿。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 2016-11-07
      相关资源
      最近更新 更多