8

私は、Windowsログインを使用してASP.NET MVC 4でイントラネットWebサイトを作成しようとしています。私は正常にWindowsのログインを完了しました。私が立ち往生している唯一の事は、部分的なユーザー名でアクティブディレクトリを検索することです。私はウェブとstackoverflowのウェブサイトを検索しようとしたが、まだ答えを見つけることができませんでした。ASP.NET Active Directory Search

DirectoryEntry directory = new DirectoryEntry("LDAP://DC=NUAXIS"); 
    string filter = "(&(cn=jinal*))"; 
    string[] strCats = { "cn" }; 
    List<string> items = new List<string>(); 
    DirectorySearcher dirComp = new DirectorySearcher(directory, filter, strCats,  SearchScope.Subtree); 
    SearchResultCollection results = dirComp.FindAll(); 
+0

ことができます私たちにあなたが部分的にユーザ名で検索を行うために使用したコード? – rene

+0

@rene投稿にコードを追加しました –

答えて

13

あなたの検索を行うことPrincipalSearcherと「例による問合せ」プリンシパルを使用することができます。

// create your domain context 
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain)) 
{ 
    // define a "query-by-example" principal - here, we search for a UserPrincipal 
    // and with the first name (GivenName) of "Jinal*" 
    UserPrincipal qbeUser = new UserPrincipal(ctx); 
    qbeUser.GivenName = "Jinal*"; 

    // create your principal searcher passing in the QBE principal  
    using (PrincipalSearcher srch = new PrincipalSearcher(qbeUser)) 
    { 
     // find all matches 
     foreach(var found in srch.FindAll()) 
     { 
     // do whatever here - "found" is of type "Principal" - 
     // it could be user, group, computer.....   
     } 
    } 
} 

あなたがまだの場合は - 絶対にうまく示しMSDNの記事Managing Directory Security Principals in the .NET Framework 3.5を読みますSystem.DirectoryServices.AccountManagementの新機能を最大限に活用する方法またはMSDN documentation on the System.DirectoryServices.AccountManagement名前空間を参照してください。

  • DisplayName(通常:最初の名+スペース+姓もちろん

    、あなたの必要性に応じて、あなたが作成する「例による問合せ」ユーザープリンシパル上の他のプロパティを指定したい場合があります)

  • SAM Account Nameから
  • User Principal NameあなたのWindows/ADアカウント名 - あなたの "[email protected]" スタイル名

あなたがSPEすることができますUserPrincipalのいずれかのプロパティを確認して、PrincipalSearcherの「クエリバイケース」として使用します。

+0

ありがとうございました。 –

0

あなたの現在のコードは正しいトラックにあります。 あなたはワイルドカードを後方に持っていたと思います。

はこのことを考えてみましょう:

search.Filter = string.Format("(&(sn={0}*)(givenName={1}*)(objectSid=*))", lastName, firstName);