2012-05-22 58 views
9

我正在使用System.DirectoryServices.ActiveDirectory類來查找所有Active Directory用戶。該代碼非常簡單:UserPrincipal對象中的域名在哪裏?

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

我想要得到的域限定用戶名中的「友好」(又名「Windows 2000以前版本」的格式。),例如。 「CONTOSO \ SmithJ」。 UserPrincipal.SamAccountName給我的用戶名部分,但我如何獲得域的一部分?我不能假定域將與機器或當前用戶的域相同。

+0

可能重複:http://stackoverflow.com/questions/4284641/get-netbiosname-from- a-userprincipal-object – MichelZ

回答

6

對於AD DS,msDS-PrincipalName的值是NetBIOS域名,後跟反斜槓(「\」)。

可以使用找到它:

/* 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謝謝,msDS-PrincipalName看起來像我想要的。 – EMP

+0

注意:它可能是域\用戶名或根據MSDN用戶的SID:http://msdn.microsoft.com/en-us/library/cc223404.aspx –

0

OK,這裏的最終代碼我想出了利用JPBlanc的答案和answer linked by MichaelZ。它顯示每個用戶的SID,顯示名稱和域名\用戶名。

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

,另外給機器的域中的默認命名上下文(如answered here):

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