2009-06-02 13 views
13

Active DirectoryのDirectoryEntry(SchemaClassName = "user")オブジェクトからWindowsユーザーとドメインを取得するにはどうすればよいですか?AD DirectoryEntryからDOMAIN USERを取得するにはどうすればよいですか?

ユーザー名はsAMAccountNameプロパティにありますが、どこでドメイン名を検索できますか?

(ユーザーがさまざまなサブドメインからあるので、私は固定されたドメイン名を想定することはできません。)

+0

関連:http://stackoverflow.com/questions/4249139/c-sharp-active-directory-get-domain-name-of-user –

答えて

3

私は、すべてのドメインが含まれているCN =パーティション、CN = Configurationのパーティションコンテナを発見しました。

ユーザーをpartionに一致させると、nETBIOSName + "\" + sAMAccountNameプロパティから実際のドメイン名を読み取ることができます。

+1

nETBIOSNameはコンピュータ名ですか?どのようにこれはドメイン名を与えるのだろうか? – Shesha

2

あなたがSystem.DirectoryServicesライブラリを使用している場合は、たDirectorySearcherからSearchResultsCollectionを持つ必要があります。

各SearchResultのPropertiesコレクションには、「distinguishedname」プロパティがあります。これには、ディレクトリエントリが属するドメインを構成するすべてのDC部分が含まれます。

+0

+1:私はこのようにすべての種類の情報を取得しました。彼らは誰に雇用されているのか、また、すべてのセキュリティグループのリストがあります。 – RSolberg

+5

はい、どうすればそれが助けになりますか? DC = Company、DC = comからドメイン名「company-central」を取得するにはどうすればよいですか? – laktak

+0

これは正しい方法です...しかし、ドメインはしばしばAD内の正しいパスのエイリアスであることを覚えています。私たちの組織では、ドメインSOUTH_AMERICAは実際にsoa.company.comです。EUROPEはdc = eur、dc = company、dc = comなどで表されるeur.company.comですので、ルックアップテーブルが必要な場合や検索が必要な場合がありますdistinguishedName文字列の – davidsleeps

7

残念なことに、DirectoryEntryで探しているものが見つかりません。

sAMAccountNameは、通常myuser(ドメインなし)のようなものです。あなたはLDAP://cn=joe myuser,cn=Users,dc=yourCompany,dc=comのようなものであるdistinguishedNameを持っています。 userPrincipalNameもありますが、通常は[email protected]という形式の名前です。

残念ながら、domain\MyUserの属性は見つかりません。あなたはドメイン名とDirectoryEntryのsAMAccountNameに関するあなたの情報からそれをまとめなければなりません。

System.DirectoryServicesのすべてのLDAPおよびWinNTプロパティに関する詳細と優れたExcelシートについては、ADSI MVP Richard MuellerのHilltop Lab Webサイトを参照してください。

マルク・

21

これは、resultsがDirectorySearcherから取得したSearchResultCollectionであると仮定していますが、直接DirectoryEntryからオブジェクトIDを取得できるはずです。

SearchResult result = results[0]; 
var propertyValues = result.Properties["objectsid"]; 
var objectsid = (byte[])propertyValues[0]; 

var sid = new SecurityIdentifier(objectsid, 0) 

var account = sid.Translate(typeof(NTAccount)); 
account.ToString(); // This give the DOMAIN\User format for the account 
+3

残念ながら、これは私にとってはうまくいきません - 私はobjectsidを持っていますが、私はTranslate()呼び出しでIdentityNotMappedExceptionを取得します。これは、コードを実行しているマシンがドメインの一部ではないためです。私はADに問い合わせています。 – laktak

+0

逆に翻訳する方法はありますか? DOMAIN \ userという形式の文字列がある場合、ディレクトリオブジェクトを検索できますか?特に信頼できる複数のドメインを扱うことができると考えると、これは可能でしょうか? – Jeremy

+0

ありがとうございます。 .NET 4.5ではまだ動作します。 – Steven

7

あなたは directoryEntry.Parentに再帰を使用することができたDirectoryEntryドメイン名を取得します。 そしてdirectoryEntry.SchemaClassName == "domainDNS" 場合は、このようなドメイン名を取得することができますが:

directoryEntry.Properties["Name"].Value 
-2

1)あなたがたDirectoryEntryからのuserPrincipalNameを得ることができます。

2)次に、ユーザー名とドメイン名の間でUPNを分割します。

3)次にGetNetBIOSName()を呼び出します。

 public static DirectoryEntry GetDirectoryObject(string strPath) 
     { 
      if (strPath == "") 
      { 
       strPath = ConfigurationManager.AppSettings["LDAPPath"]; //YOUR DEFAULT LDAP PATH ie. LDAP://YourDomainServer 
      } 

      string username = ConfigurationManager.AppSettings["LDAPAccount"]; 
      string password = ConfigurationManager.AppSettings["LDAPPassword"]; 
       //You can encrypt and decrypt your password settings in web.config, but for the sake of simplicity, I've excluded the encryption code from this listing. 

} 
      catch (Exception ex) 
      { 
       HttpContext.Current.Response.Write("user: " + username + ", LDAPAccount: "+ ConfigurationManager.AppSettings["LDAPAccount"] + ".<br /> "+ ex.Message +"<br />"); 

       if (HttpContext.Current.User.Identity != null) 
       { 

        HttpContext.Current.Response.Write("HttpContext.Current.User.Identity: " + HttpContext.Current.User.Identity.Name + ", " + HttpContext.Current.User.Identity.IsAuthenticated.ToString() + "<br />"); 

        HttpContext.Current.Response.Write("Windows Identity: " + WindowsIdentity.GetCurrent().Name + ", " + HttpContext.Current.User.Identity.IsAuthenticated.ToString()); 


       } 
       else 
       { 
        HttpContext.Current.Response.Write("User.Identity is null."); 
       } 

       HttpContext.Current.Response.End(); 


      } 




      DirectoryEntry oDE = new DirectoryEntry(strPath, username, password, AuthenticationTypes.Secure); 
      return oDE; 
     } 




public static string GetNetBIOSName(string DomainName) 
{ 



    string netBIOSName = ""; 
    DirectoryEntry rootDSE =GetDirectoryObject(
     "LDAP://"+DomainName+"/rootDSE"); 

    string domain = (string)rootDSE.Properties[ 
     "defaultNamingContext"][0]; 

     // netBIOSName += "Naming Context: " + domain + "<br />"; 

    if (!String.IsNullOrEmpty(domain)) 
    { 

      //This code assumes you have a directory entry at the /CN=Partitions, CN=Configuration 
      //It will not work if you do not have this entry. 

     DirectoryEntry parts = GetDirectoryObject(
      "LDAP://"+DomainName+"/CN=Partitions, CN=Configuration," + domain); 

      foreach (DirectoryEntry part in parts.Children) 
     { 


      if ((string)part.Properties[ 
       "nCName"][0] == domain) 
      { 
       netBIOSName += (string)part.Properties[ 
        "NetBIOSName"][0]; 
       break; 
      } 
     } 


    } 
     return netBIOSName; 
} 


    public static string GetDomainUsernameFromUPN(string strUPN) 
{ 
string DomainName; 
string UserName; 
    if (strUPN.Contains("@")) 
     { 
      string[] ud = strUPN.Split('@'); 
      strUPN= ud[0]; 
      DomainName = ud[1]; 

      DomainName=LDAPToolKit.GetNetBIOSName(DomainName); 

      UserName= DomainName + "\\" + strUPN; 
     } 
     else 
     { 
      UserName= strUPN; 
     } 


    return UserName; 
} 
3
public static string GetDomainNameUserNameFromUPN(string strUPN) 
{ 

    try 
    { 
     WindowsIdentity wi = new WindowsIdentity(strUPN); 
     WindowsPrincipal wp = new WindowsPrincipal(wi); 

     return wp.Identity.Name; 



    } 
    catch (Exception ex) 
    { 

    } 

    return ""; 
} 
0

は、私は彼が何を意味するのかの詳細を提供するために@laktakで前の回答を延長しています。

あなたのNetBIOSドメイン名と、彼らは、このドメインにある場合、ユーザーが持つことになりますdistinguishedName接頭辞が含まれていnCNameプロパティですcnを与えるすべてのドメインが含まれていCN=Partitions,CN=Configuration内のパーティションコンテナがあります。

だからCN=Partitions,CN=Configuration(objectClass=*)のLDAPを検索することによって開始し、マップに各結果の(cnnCName)ペアを格納します。

次に、(sAMAccountName=USERIDHERE)を使用してldapに問い合わせ、distinguishedNameを取得します。今すぐ(cnnCName)のペアを調べ、distinguishedNameという接頭辞を持つnCNameが見つかると、対応するcnが目的のドメイン名になります。

関連する問題