由网友(请止步、禁区)分享简介:我有搜索的基础上的电子邮件地址,用户名Active Directory中的方法。有些情况下可能有多个用户名对一个给定的电子邮件地址,我试图捕捉那些。我已经重写我的方法,但似乎无法得到语法完全正确。问题是这条线,我相信。的foreach(在result.Properties对象myObject的[属性])感谢杰森私人字符...
我有搜索的基础上的电子邮件地址,用户名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
相关推荐
最新文章