Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Active Directory中搜索与电子邮件地址关联的所有用户名_C#_Active Directory - Fatal编程技术网

C# 在Active Directory中搜索与电子邮件地址关联的所有用户名

C# 在Active Directory中搜索与电子邮件地址关联的所有用户名,c#,active-directory,C#,Active Directory,我有一个基于电子邮件地址搜索Active Directory用户名的方法。在某些情况下,给定的电子邮件地址可能有多个用户名,我正在尝试捕获这些用户名。我已经重写了我的方法,但似乎不能完全正确地使用语法。问题是我相信这条线 foreach (Object myObject in result.Properties[property]) 谢谢 杰森 编辑:将其更改为输出到字符串列表 我们开始: List<string> usernames = new List<string>

我有一个基于电子邮件地址搜索Active Directory用户名的方法。在某些情况下,给定的电子邮件地址可能有多个用户名,我正在尝试捕获这些用户名。我已经重写了我的方法,但似乎不能完全正确地使用语法。问题是我相信这条线

foreach (Object myObject in result.Properties[property])
谢谢

杰森


编辑:将其更改为输出到字符串列表

我们开始:

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();
List usernames=newlist();
如果(结果!=null)
{
foreach(搜索结果sr在结果中)
{
usernames.Add((string)sr.Properties[“SAMAccountName”][0]);
}
}
listBox1.DataSource=用户名//在标记中声明listbox的位置
listBox1.DataBind();

只需将if(result!=null)逻辑替换为myne

谢谢您的回复。它现在返回active directory中与电子邮件地址关联的最后一个用户名。我只有6个用户,其中4个与电子邮件关联。当我运行调试并逐步查看结果时,我可以看到我期望的每个用户名。但只有最后一个正在写入。这将在屏幕响应上写入所有用户。写入(users+“
”@Jason,这是因为您正在将其写入列表框,将其更改为输出到某种列表,我将相应地更新代码再次感谢您的帮助。我收到一个错误,与System.Collections.Generic.List.Add(string)”匹配的最佳重载方法具有一些无效参数
GetDirectoryEntry()
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();