2012-04-11 24 views
7

Bir Active Directory kullanıcısı listesi döndüren bir yöntem uyguladım, bu Domain\Administrator gibi SAMAccountName değerini almak istiyorum.SAMAccountName Active Directory'den nasıl geri alınır

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; 
} 

cevap

13

Öncelikle:Domain\AdministratorDEĞİL bir SAM hesap adı olan

Bu

kullandığım yöntemdir! SAM hesap adı, 20 karakter uzunluğunda bir benzersiz (tüm etki alanı üzerinde) adıdır - genellikle "Windows kullanıcı adınız" (ör. Administrator) - ancak değil etki alanı adını içerir. domain\username'dan oluşan bu değer, Active Directory'de her yerde saklanan NOT'dur!


.NET 3.5 ve yukarı iseniz, System.DirectoryServices.AccountManagement (S.DS.AM) ad kontrol etmeliyiz. Hepsi burada! okuyunuz:

// 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; 
} 

: MSDN docs on System.DirectoryServices.AccountManagement

Temelde

  • Managing Directory Security Principals in the .NET Framework 3.5
  • , sen AD kullanıcıları ve/veya grupları bulmak kolay etki alanı bağlamını tanımlayabilir ve Yeni S.DS.AM, AD'deki kullanıcılarla ve gruplarla oynamayı gerçekten çok kolaylaştırıyor!

    // 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.....   
    } 
    
1

You:

Eğer arama yapmak için kullanıcılar (veya gruplar veya bilgisayarlar) bir bütün grup, bir PrincipalSearcher ve "sorgusu Örnekle" kullanmak başlıca edebilirsiniz aramak istiyorsanız

Nesnenin SID'sini ve System.Security.Principal.SecurityIdentifier.Translate komutunu kullanarak bir kullanıcıyı DOMAIN \ SAMaccount formuna Ayırt edici bir ad olarak çevirebilir.

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; 
    } 
+0

"Bazı veya tüm kimlik başvuruları çevrilemedi." çeviri hatası. – Shesha

+0

Çok alanlı orman? Reklam bağlantınızın ormanınızın GC olduğuna emin olmalısınız. Varlığınızın gerçekten bir yetim olması da olabilir. –

İlgili konular