我正在寻找一种方法来获取当前登录用户的完整Active Direcory LDAP路径。例如
LDAP://CN=john.smith,OU=UK,OU=Sales,DC=Company,DC=local
答案 0 :(得分:3)
使用此过滤器查询LDAP目录(例如AD):
(&(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]