2011-04-01 80 views

回答

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; 
} 
+0

@rmcdonald:用这个代码我可以看到用户名和昵称? – Farna 2011-04-01 18:37:56

+0

对不起,要添加其他字段只需使用myDirectorySearcher.PropertiesToLoad.Add()。例如,要获得givenname(我认为是昵称): myDirectorySearcher.PropertiesToLoad.Add(“givenname”) – Porco 2011-04-01 18:50:23

+0

@rmcdonald:我是新程序员,对不起,您的意思是我可以将2个属性添加到myDirectorySearcher ?? myDirectorySearcher。 PropertiesToLoad.Add( “CN”); 和myDirectorySearcher.PropertiesToLoad.Add(“givenname”)好吗? – Farna 2011-04-01 18:56:54

相关问题