12

如何通过电子邮件地址查询 ActiveDirectory 用户?给定用户可以拥有多个电子邮件,例如 john.smite@acme.com 和 jsmith@acme.com。对于给定的电子邮件,我如何取回 A/D 用户?

我正在用 C# 编程。

4

3 回答 3

25

您可以使用以下代码搜索您的广告:

    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());
        }
    }

€:这将在 proxyAddresses 中搜索托管所有邮件地址的邮件地址

    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;
    }
于 2013-09-06T13:19:36.810 回答
2

我不知道我是否在这里遗漏了什么,但是从电子邮件地址查找 AD 用户可以更简单:

var context = new PrincipalContext(ContextType.Domain);
var userPrincipal = UserPrincipal.FindByIdentity(context, emailAddress);
于 2019-10-30T10:56:52.310 回答
-2

找到了一个简单的答案:

WindowsIdentity identity = WindowsIdentity.GetCurrent();
UserPrincipal userPrincipal = UserPrincipal.Current;
string email = userPrincipal.EmailAddress;

这就是所有需要的。

于 2019-10-30T15:56:38.730 回答