2009-03-09 65 views
1

ユーザー名とドメインに基づいてユーザーのフルネームを取得する関数があります。この関数は、偽装されたユーザーのASP.NETスレッドで実行されます。 リモートADブランチでDirectory searcherを使用すると、プロパティの代わりにSID番号が取得されていると考えられます(別のボックスで確認できません)。Active Directory検索で遅延バインディングを克服する方法

public string GetUserFullName(string userName, string domainName) 
{ 
    DirectoryEntry rootEntry = new DirectoryEntry("GC://dc=company,dc=net"); 
    string filter = string.Format("(&(objectCategory=person)(objectClass=user)(!(userAccountControl:1.2.840.113556.1.4.803:=2))(userPrincipalName={0}@{1}.company.net))", userName, domainName); 
    DirectorySearcher searcher = new DirectorySearcher(rootEntry, filter, new string[] { "displayName" }); 
    rootEntry.AuthenticationType = AuthenticationTypes.Secure; 
    searcher.PageSize = 1000; 
    searcher.ServerTimeLimit = new TimeSpan(0, 10, 0); 
    searcher.ReferralChasing = ReferralChasingOption.All; 
    searcher.Asynchronous = false; 

    SearchResult result = searcher.FindOne(); 
    if (result != null) 
    { 
     return (string) result.Properties["displayName"][0]; 
    } 
    else 
    { 
     throw new Exception("Active Directory could not resolve your user name"); 
    } 

} 

答えて

3

.NET Frameworkのどのバージョンを使用していますか? ADのものは.NET 3.5で大幅に改良されており、ユーザーやグループのために強く型付けされた構造体を提供しています。

MSDNの仲間Joe KaplanとEthan Wilanskyが優れた記事 "Managing Directory Security Principals in the .NET Framework 3.5"をチェックしてください。確かに優れたもの。

まず、強く型付けされたUserPrincipalというクラスを取得します。すべての基本プロパティはオブジェクトのプロパティです。確かに非常に役立つ。

第二に、あなたはPrincipalSearcherを使用して素敵な「例による問合せ」メソッドを取得 - ジョーとイーサンの記事からこのサンプルをチェックアウト:

// create a principal object representation to describe 
// what will be searched 
UserPrincipal user = new UserPrincipal(adPrincipalContext); 

// define the properties of the search (this can use wildcards) 
user.Enabled = false; 
user.Name = "user*"; 

// create a principal searcher for running a search operation 
PrincipalSearcher pS = new PrincipalSearcher(); 

// assign the query filter property for the principal object 
// you created 
// you can also pass the user principal in the 
// PrincipalSearcher constructor 
pS.QueryFilter = user; 

// run the query 
PrincipalSearchResult<Principal> results = pS.FindAll(); 

Console.WriteLine("Disabled accounts starting with a name of 'user':"); 
foreach (Principal result in results) 
{ 
    Console.WriteLine("name: {0}", result.Name); 
} 

をまったくチャンスがある場合に取得しよう。あなたのADのもののためのNET 3.5!

Marc

+0

It's NET 2.0です。これを指摘してくれてありがとう! エイドリアン –

0

私は便利なヘルパーライブラリにADを包んきたし、常にこの方法を使用しています

/// <summary> 
    /// Returns AD information for a specified userID. 
    /// </summary> 
    /// <param name="ntID"></param> 
    /// <returns></returns> 
    public ADUser GetUser(string ntID) 
    {   
     DirectorySearcher search = new DirectorySearcher();   

     search.Filter = String.Format("(cn={0})", ntID); 

     search.PropertiesToLoad.Add("mail"); 
     search.PropertiesToLoad.Add("givenName"); 
     search.PropertiesToLoad.Add("sn"); 
     search.PropertiesToLoad.Add("displayName"); 
     search.PropertiesToLoad.Add("userPrincipalName"); 
     search.PropertiesToLoad.Add("cn"); 

     SearchResult result = search.FindOne(); 

     return new ADUser(result); 
    } 

ADUserは強い型指定されたプロパティへのSearchResultをマップするカスタムクラスです。

私はあなたの特定の問題が何であるか分かりませんが、これはいつも私のために働いています。

編集:私たちのコードを比較すると、私はあなたがプロパティをプリロードする検索を言っていない参照してください...それはおそらくあなたの問題です。

+0

こんにちはFlySwat、DirectorySearcherコンストラクタには、配列{{displayName}}を読み込むためのプロパティがあります。私はADUserクラスを使ってみます。ありがとうございます、 –

+0

ADUserは、私はユーザーを少し穏やかに扱うように書いたクラスです。 – FlySwat

関連する問題