using System.DirectoryServices;
static void Main(string[] args)
{
string Username = "0MXXX";
string Password = "AbXXX";
DirectoryEntry entry = new DirectoryEntry("ADserver.domain.com.tw");
entry.Path = "LDAP://DC=domain,DC=com,DC=tw";
DirectorySearcher search = new DirectorySearcher(entry);
/* Must using 'SAMAccountName' here */
search.Filter = "(&(SAMAccountName=" + Username + "))";
SearchResult result = search.FindOne();
string DisplayName = (String)result.Properties["displayname"][0];
System.Console.WriteLine(DisplayName);
}
記得透過 DirectorySearcher.Filter 指定搜尋 SAMAccountName 鍵值;回傳的結果陣列中 displayname 則是使用者登入 AD 後顯示名稱。
沒有留言:
張貼留言