从本地网络上的活动目录中检索用户名和昵称

时间:2011-04-01 18:32:14

标签: c# winforms networking active-directory

我想从服务器的活动目录中读取本地网络上任何用户的用户名和NickName。我该怎么做?非常感谢。

1 个答案:

答案 0 :(得分:1)

public void getUser()
{
 DirectoryServices.SearchResult myResult;
 string filterString = string.Empty;
 string EntryString = "LDAP:// <Your AD Domain here>";
 DirectoryServices.DirectorySearcher myDirectorySearcher = new DirectoryServices.DirectorySearcher(new DirectoryServices.DirectoryEntry(EntryString, "Username", "Password"));
 string tempStr;
 string[] splStr = new string[3];

 filterString = "(sAMAccountName=" + Username + ")";
 myDirectorySearcher.Filter = filterString;
 myDirectorySearcher.PropertiesToLoad.Add("cn");
 myResult = myDirectorySearcher.FindOne();
 splStr = Regex.Split(myResult.Properties("cn").Item(0).ToString, " ");
 tempStr = splStr(1).ToString + " " + splStr(0).ToString;
 Label1.Text = "Hello " + tempStr;
}