开发者

Search Active Directory for all Usernames associated with email address

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)
          开发者_如何学JAVA 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜