2
我正在尋找一種獲取當前登錄用戶的完整Active Direcory LDAP路徑的方法。例如如何獲取C#中登錄用戶的OU?
LDAP://CN=john.smith,OU=UK,OU=Sales,DC=Company,DC=local
我正在尋找一種獲取當前登錄用戶的完整Active Direcory LDAP路徑的方法。例如如何獲取C#中登錄用戶的OU?
LDAP://CN=john.smith,OU=UK,OU=Sales,DC=Company,DC=local
查詢LDAP目錄(例如,廣告),用此過濾器:
(&(objectCategory=user)(sAMAccountName=<user-logon-name-here>))
返回的對象的DN是你正在尋找的東西。
事情是這樣的:
DirectorySearcher ds = new DirectorySearcher();
string userName = WindowsIdentity.GetCurrent().Name;
string userFilter = "(&(objectCategory=user)(sAMAccountName={0}))";
ds.SearchScope = SearchScope.Subtree;
ds.PropertiesToLoad.Add("distinguishedName");
ds.PageSize = 1;
ds.ServerPageTimeLimit = TimeSpan.FromSeconds(2);
ds.Filter = string.Format(userFilter, userName);
SearchResult sr = ds.FindOne();
// now do something with sr.Properties["distinguishedName"][0]