2013-09-06 20 views
8

電子メールアドレスでActiveDirectoryユーザーに問い合わせるにはどうすればよいですか?特定のユーザーは、[email protected][email protected]のような複数の電子メールを持つことができます。 Eメールの場合、どのようにしてA/Dユーザーを取り戻すことができますか?電子メールアドレスでActiveDirectoryを検索する

私はC#でプログラミングしています。

答えて

17

あなたは、次のコードを使用してADを検索することができます。

DirectoryEntry adEntry = null; 

    private void SetADInfoAndCredentials() 
    { 
     adEntry = new DirectoryEntry("LDAP://" + ad_textBox.Text); 
     adEntry.Username = user_textBox.Text; 
     adEntry.Password = pw_textBox.Text; 
    } 

    private void SearchForMailInAD() 
    { 
     DirectorySearcher adSearcher = new DirectorySearcher(adEntry); 
     adSearcher.Filter = ("mail=" + mail_textBox.Text); 
     SearchResultCollection coll = adSearcher.FindAll(); 
     foreach (SearchResult item in coll) 
     { 
      foundUsers_listBox.Items.Add(item.GetDirectoryEntry()); 
     } 
    } 

€:これは、すべてのメールアドレス

public static SearchResultCollection FindAccountByEmail(string pEmailAddress) 
    { 
     string filter = string.Format("(proxyaddresses=SMTP:{0})", email); 

     using (DirectoryEntry gc = new DirectoryEntry("LDAP:")) 
     { 
      foreach (DirectoryEntry z in gc.Children) 
      { 
       using (DirectoryEntry root = z) 
       { 
        using (DirectorySearcher searcher = new DirectorySearcher(root, filter, new string[] { "proxyAddresses", "objectGuid", "displayName", "distinguishedName" })) 
        { 
         searcher.ReferralChasing = ReferralChasingOption.All; 
         SearchResultCollection result = searcher.FindAll(); 

         return result; 
        } 
       } 
      } 
     } 
     return null; 
    } 
+0

をホストするのproxyAddressesでメールアドレスを検索するには、ありがとう - 第二のコードセットは素晴らしい作品。最初の(LDAP)アプローチをいつ使うのですか?そして、LDAP://? –

+0

最初のコードは、どのプライマリメールアドレスが定義されているかを正確に把握するだけです。 Ldapの後、ドメインやコントローラを設定する必要があります –

関連する問題