Active Directory中搜索与电子邮件地址相关联的所有用户名 [英] Search Active Directory for all Usernames associated with email address
本文介绍了Active Directory中搜索与电子邮件地址相关联的所有用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有搜索的基础上的电子邮件地址,用户名Active Directory中的方法。有些情况下可能有多个用户名对一个给定的电子邮件地址,我试图捕捉那些。我已经重写我的方法,但似乎无法得到语法完全正确。问题是这条线,我相信。
的foreach(在result.Properties对象myObject的[属性])
感谢
杰森
私人字符串FindNameByEmail(字符串EMAILADDRESS)
{
的DirectoryEntry条目= GetDirectoryEntry();
EMAILADDRESS = txtEmailID.Text;
DirectorySearcher从搜索=新DirectorySearcher从(输入);
search.Filter =(及(objectCategory属性=人)(sAMAccountName赋= *)(邮件=+ EMAILADDRESS +));
字符串[]属性=新的String [] {SAM帐户名};
的foreach(在属性字符串属性)
search.PropertiesToLoad.Add(属性);
SearchResultCollection结果= search.FindAll();
如果(结果!= NULL)
{
的foreach(在属性字符串属性)
的foreach(在result.Properties对象myObject的[属性])
lblUserName.Text = myObject.ToString();
返回用户发现;
}
其他
{
lblStatus.Text =用户不存在;
返回用户不存在;
}
}
解决方案
编辑:它改变输出到字符串列表
在这里,我们去:
名单,其中,串>用户名=新的名单,其中,串>();
如果(结果!= NULL)
{
的foreach(在结果信息搜索结果SR)
{
usernames.Add((字符串)sr.Properties [SAM帐户名] [0]);
}
}
listBox1.DataSource =用户名; //如果列表框在你的标记声明
listBox1.DataBind();
只需更换你的,如果(结果!= NULL)与MYNE逻辑
I have a method that searches Active Directory for Usernames based on an email address. There are cases where there may be multiple usernames for a given email address, and I'm trying to capture those. I have rewritten my method, but can't seem to get the syntax quite right. the issue is this line I believe.
foreach (Object myObject in result.Properties[property])
thanks,
Jason
private String FindNameByEmail(string emailAddress)
{
DirectoryEntry entry = GetDirectoryEntry();
emailAddress = txtEmailID.Text;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(&(objectCategory=person)(sAMAccountName=*)(mail=" + emailAddress + "))";
string[] properties = new string[] { "SAMAccountName" };
foreach (String property in properties)
search.PropertiesToLoad.Add(property);
SearchResultCollection result = search.FindAll();
if (result != null)
{
foreach (String property in properties)
foreach (Object myObject in result.Properties[property])
lblUserName.Text = myObject.ToString();
return "User Found";
}
else
{
lblStatus.Text = "user does not exist";
return "User Does Not Exist";
}
}
解决方案
EDIT: Changed it to output to a list of strings
Here we go:
List<string> usernames = new List<string>();
if (result != null)
{
foreach (SearchResult sr in result)
{
usernames.Add((string)sr.Properties["SAMAccountName"][0]);
}
}
listBox1.DataSource = usernames; //Where listbox is declared in your markup
listBox1.DataBind();
Just Replace your if (result != null) logic with myne
这篇关于Active Directory中搜索与电子邮件地址相关联的所有用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文