2016-12-27 4 views
-1

LDAPサーバーのActive Directoryから特定のグループのすべてのユーザーを取得しようとしています。認証は成功しますが、私は結果がnullになっています。 以下は私のコードです。Active DirectoryユーザーがMVCでインポートする

ドメイン172.11.12.123
[email protected]
パスワード-123456

using (var context = new DirectoryEntry(user.Domain, user.Email, user.Password, AuthenticationTypes.Secure)) 
      { 

       try 
       {      
        string FirstName; 
        string LastName; 
        string ADUserName; 
        string Email; 

        using (var searcher = new DirectorySearcher(context)) 
        { 
         searcher.Filter = "(&((&(objectCategory=Person)(objectClass=User)))(samaccountname='user3'))"; 
         List<string> Adusers = new List<string>(); 
         System.DirectoryServices.SearchResult result = searcher.FindOne(); 




        } 
       } 
       catch (Exception ex) 
       { 
        TempData["message"] = "error"; 
        return RedirectToAction("Index", "ADuserList"); 
       } 

      } 

で何が起こっているのか間違っています。 ありがとうございます

+0

と'dsa.msc'を起動し、検索を行ってください。構文は幾分奇妙に見えます(2つのorsと1つのors、afaicちょうど1つしかありませんでした) – zaitsman

+0

DirectoryEntry ctorの最初の引数がLDAPであることを確認してください://172.11.12.123 filter samaccountname = 'user3'を使用して検索を実行しますActive Directoryのユーザー "user3"に対してこのユーザーが存在することを確認します。また、一重引用符は必要ありません。単にsamaccountname = user3と入力してください。 – oldovets

+0

ありがとうございます。私は正しい形式を教えてください。 –

答えて

0

.NET 3.5以降の場合は、System.DirectoryServices.AccountManagement(S.DS.AM)名前空間をチェックアウトする必要があります。

基本的に、あなたはドメインコンテキストを定義し、簡単にADのユーザーおよび/またはグループを見つけることができます:

// set up domain context for the currently connected AD domain 
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain)) 
{ 
    // find the group in question 
    GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere"); 

    // if found.... 
    if (group != null) 
    { 
     // iterate over members 
     foreach (Principal p in group.GetMembers()) 
     { 
      Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName); 
      // do whatever you need to do to those members 
     } 
    } 
} 

新しいS.DS.AMは、ADのユーザーとグループで遊んすることが本当に簡単になります!

ここでそれについて詳しく読む:

を更新:与えられたOUのすべてのユーザーを取得するために、アプローチはかなり異なっています。あなたはOUは、あなたが興味を持っているかを定義し、そのPrincipalContext別々に作成する必要が

- そして、あなたはそのOUからすべてのユーザーを取得するためにPrincipalSearcherを使用する必要があります:あなたは、DCへのRDP場合

// create your domain context and define what OU to use: 
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, null, "OU=YourOU,OU=SubOU,dc=YourCompany,dc=com")) 
{ 
    // define a "query-by-example" principal - here, we search for any UserPrincipal 
    UserPrincipal qbeUser = new UserPrincipal(ctx); 

    // create your principal searcher passing in the QBE principal  
    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.....   
    } 
} 
+0

うわー、ありがとう。それは魅力のように働いた。しかし、OU(組織単位)をグループに渡しているときは機能しません。 OUの場合に何をすべきか。 –

+0

@ C.jacking:うーん、「OU」はグループではありません。だから、これはOUのためのものではありません。もちろん、あなたは何をしようとしていますか? OU内のすべてのユーザーを一覧表示しますか?または、あなたのADツリーでOUを見つける?それともほかに何か? –

+0

特定のOUとグループのすべてのユーザーの一覧が必要です。グループの問題はあなたのコードによって解決されましたが、OUに従ってリストも欲しいです。 –

関連する問題