2012-05-22 13 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によれば、Domain \ UsernameまたはユーザーのSIDになる可能性があります。http://msdn.microsoft.com/en-us/library/cc223404.aspx –

0

OK、」私はJPBlancの答えとanswer linked by MichaelZを使用して思い付いた最終的なコードを。各ユーザーのSID、表示名、DOMAIN \ユーザー名が表示されます。

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; 
     } 
    } 
} 
関連する問題