C# 通过电子邮件地址在 ActiveDirectory 中查找用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18658345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:52:41  来源:igfitidea点击:

lookup user in ActiveDirectory by email address

c#active-directory

提问by David Thielen

How can I query an ActiveDirectory user by email address? A given user can have multiple emails such as both [email protected] and [email protected]. For a given email, how can I get back the A/D user?

如何通过电子邮件地址查询 ActiveDirectory 用户?一个给定的用户可以有多个电子邮件,例如 [email protected][email protected]。对于给定的电子邮件,我如何找回 A/D 用户?

I'm programming in C#.

我正在用 C# 编程。

采纳答案by David Thielen

Found a simple answer:

找到一个简单的答案:

WindowsIdentity identity = WindowsIdentity.GetCurrent();
UserPrincipal userPrincipal = UserPrincipal.Current;

That's all that's needed.

这就是所需要的。

回答by Daniel Abou Chleih

You can search your AD with following code:

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

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

: This will search for the mail address in proxyAddresses which hosts all mail addresses

: 这将在 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;
    }

回答by Oskar Lundgren

I don't know if I am missing something here, but finding an AD user from an e-mail address can be done much simpler:

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

var context = new PrincipalContext(ContextType.Domain);
var userPrincipal = UserPrincipal.FindByIdentity(context, emailAddress);