C# 按电子邮件地址查找ActiveDirectory中的用户
如何通过电子邮件地址查询ActiveDirectory用户?一个给定的用户可以有多封电子邮件,如john和john。smite@acme.com及jsmith@acme.com. 对于给定的电子邮件,我如何取回a/D用户C# 按电子邮件地址查找ActiveDirectory中的用户,c#,active-directory,C#,Active Directory,如何通过电子邮件地址查询ActiveDirectory用户?一个给定的用户可以有多封电子邮件,如john和john。smite@acme.com及jsmith@acme.com. 对于给定的电子邮件,我如何取回a/D用户 我正在用C#编程。您可以使用以下代码搜索您的广告: DirectoryEntry adEntry = null; private void SetADInfoAndCredentials() { adEntry = new Direct
我正在用C#编程。您可以使用以下代码搜索您的广告:
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;
}
我不知道我是否遗漏了什么,但从电子邮件地址查找广告用户可以简单得多:
var context = new PrincipalContext(ContextType.Domain);
var userPrincipal = UserPrincipal.FindByIdentity(context, emailAddress);
找到了一个简单的答案:
WindowsIdentity identity = WindowsIdentity.GetCurrent();
UserPrincipal userPrincipal = UserPrincipal.Current;
string email = userPrincipal.EmailAddress;
这就是所需要的。谢谢-第二个代码集非常有用。我希望何时使用第一种(LDAP)方法?LDAP之后的内容://?第一个代码只能用于准确地知道定义了哪个主邮件地址。在Ldap之后,您必须可以设置域或控制器为什么要使用foreach循环?您在第一次迭代中返回一个值。您是如何确定必须使用筛选器
mail=
?不在电子邮件下存储电子邮件吗?UserPrincipal.EmailAddress是。给定电子邮件地址,此代码如何帮助查找用户?对我来说,这仅适用于用户名,不适用于电子邮件。为什么?