2011-04-01 39 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

相關問題