2012-04-11 39 views
7

Ho implementato un metodo che restituisce un elenco di utenti di Active Directory, vorrei recuperare SAMAccountName come questo Domain\Administrator.Come recuperare SAMAccountName da Active Directory

Questo è il metodo che uso:

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

risposta

13

Prima di tutto:Domain\Administrator NON è un account SAM nome! Il nome dell'account SAM è un nome univoco (sull'intero dominio) con una lunghezza massima di 20 caratteri, in genere il tuo "nome utente Windows" (ad esempio Administrator), ma lo NON include il nome del dominio. Il valore composto da è NON memorizzato in Active Directory ovunque!


Se siete su .NET 3.5 e fino, si dovrebbe verificare la (S.DS.AM) namespace System.DirectoryServices.AccountManagement. Leggi tutto su di esso qui:

In sostanza, è possibile definire un contesto di dominio e trovare facilmente gli utenti e/o gruppi in 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; 
} 

Il il nuovo S.DS.AM rende davvero facile giocare con utenti e gruppi in AD!

Se si desidera cercare un intero gruppo di utenti (o gruppi o computer), è possibile utilizzare una PrincipalSearcher e un "Query-by-example" principale per fare la tua ricerca:

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

È può tradurre un utente come Nome distinto in DOMAIN \ Modulo SAMaccount utilizzando il SID dell'oggetto e il comando System.Security.Principal.SecurityIdentifier.Translate.

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

Sto ricevendo "Alcuni o tutti i riferimenti di identità non possono essere tradotti." errore in traduzione. – Shesha

+0

Foresta multidominio? Assicurati che la tua connessione pubblicitaria sia al GC della foresta. Potrebbe anche essere che la tua entità sia davvero orfana. –

Problemi correlati