2012-05-22 26 views
9

Tüm Active Directory kullanıcılarını bulmak için System.DirectoryServices.ActiveDirectory sınıfları kullanıyorum. Kod çok basit:UserPrincipal nesnesindeki etki alanı adı nerede?

var context = new PrincipalContext(ContextType.Domain); 
var searcher = new PrincipalSearcher(new UserPrincipal(context)); 
var results = searcher.FindAll(); 

Ben mesela, "dost" (aka. "Windows 2000 öncesi" biçiminde) bulunan alan nitelikli kullanıcı adı almak istiyorum. "CONTOSO \ SmithJ". UserPrincipal.SamAccountName bana kullanıcı adı bölümünü veriyor, ancak etki alanı bölümünü nasıl alabilirim? Alanın, makinenin veya geçerli kullanıcının etki alanıyla aynı olacağını varsayamam.

+0

Olası yinelenen: http://stackoverflow.com/questions/4284641/get-netbiosname-from- a-userprincipal-object – MichelZ

cevap

6

AD DS için, msDS-PrincipalName değeri NetBIOS etki alanı adıdır, ardından ters eğik çizgi ("\") gelir.

kullanarak bulabilirsiniz:

/* Retreiving the root domain attributes 
*/ 
sFromWhere = "LDAP://DC_DNS_NAME:389/dc=dom,dc=fr"; 
DirectoryEntry deBase = new DirectoryEntry(sFromWhere, "AdminLogin", "PWD"); 

DirectorySearcher dsLookForDomain = new DirectorySearcher(deBase); 
dsLookForDomain.Filter = "(objectClass=*)"; 
dsLookForDomain.SearchScope = SearchScope.base; 
dsLookForDomain.PropertiesToLoad.Add("msDS-PrincipalName"); 

SearchResult srcDomains = dsLookForDomain.FindOne(); 
+0

+1 Teşekkürler, msDS-PrincipalName istediğim gibi görünüyor. – EMP

+0

Not: Bu, MSDN'ye göre Etki Alanı \ Kullanıcı Adı veya kullanıcının SID'si olabilir: http://msdn.microsoft.com/en-us/library/cc223404.aspx –

0

Tamam, işte son kod ben JPBlanc yanıtını ve answer linked by MichaelZ kullanarak ortaya attı. Her kullanıcı için SID, Görünen Ad ve DOMAIN \ kullanıcı adını gösterir.

var ldapUrl = "LDAP://" + defaultNamingContext; 

    using (var rootDe = new DirectoryEntry(ldapUrl)) 
    using (var searcher = new DirectorySearcher(rootDe)) 
    { 
     searcher.SearchScope = SearchScope.Subtree; 
     searcher.PropertiesToLoad.Add("objectSid"); 
     searcher.PropertiesToLoad.Add("displayName"); 
     searcher.PropertiesToLoad.Add("msDS-PrincipalName"); 
     searcher.Filter = "(&(objectClass=user)(objectCategory=person))"; 

     var results = searcher.FindAll(); 

     foreach (SearchResult result in results) 
     { 
      var qualifiedUsername = GetSinglePropertyValue(result, "msDS-PrincipalName"); 
      var displayName = GetSinglePropertyValue(result, "displayName"); 
      var sid = new SecurityIdentifier((byte[])GetSinglePropertyValue(result,"objectSid"), 0); 

      Console.WriteLine("User: {0}\r\n\tDisplay name: {1}\r\n\tSID: {2}", 
       qualifiedUsername, displayName, sid); 
     } 
    } 

    private static object GetSinglePropertyValue(SearchResult result, string propertyName) 
    { 
     var value = result.Properties[propertyName]; 
     if (value.Count == 0) 
      return null; 
     if (value.Count == 1) 
      return value[0]; 
     throw new ApplicationException(string.Format("Property '{0}' has {1} values for {2}", 
      propertyName, value.Count, result.Path)); 
    } 

Ve (answered here gibi) makinenin etki alanı için varsayılan adlandırma bağlamı almak için:

private static string GetDefaultNamingContext() 
{ 
    // This check is fast 
    try 
    { 
     Domain.GetComputerDomain(); 
    } 
    catch (ActiveDirectoryObjectNotFoundException) 
    { 
     return null; 
    } 

    // This takes 5 seconds if the computer is not on a domain 
    using (var rootDe = new DirectoryEntry("LDAP://RootDSE")) 
    { 
     try 
     { 
      return (string)rootDe.Properties["defaultNamingContext"][0]; 
     } 
     catch (COMException ex) 
     { 
      if (ex.ErrorCode == -2147023541) 
       return null; 
      throw; 
     } 
    } 
}