問題描述
我正在使用 System.DirectoryServices.ActiveDirectory
類來查找所有 Active Directory 用戶.代碼很簡單:
I'm using the System.DirectoryServices.ActiveDirectory
classes to find all Active Directory users. The code is very simple:
var context = new PrincipalContext(ContextType.Domain);
var searcher = new PrincipalSearcher(new UserPrincipal(context));
var results = searcher.FindAll();
我想以友好"(又名Windows 2000 之前"格式)獲取域限定用戶名,例如.CONTOSOSmithJ".UserPrincipal.SamAccountName
給了我用戶名部分,但我如何獲得域部分?我不能假設域將與機器或當前用戶的域相同.
I want to get the domain-qualified username in the "friendly" (aka. "pre-Windows 2000" format), eg. "CONTOSOSmithJ". UserPrincipal.SamAccountName
gives me the username part, but how do I get the domain part? I cannot assume that the domain will be the same as the machine's or current user's domain.
推薦答案
對于 AD DS,msDS-PrincipalName
的值是 NetBIOS 域名,后跟一個反斜杠(").
For AD DS, the value of msDS-PrincipalName
is the NetBIOS domain name, followed by a backslash ("").
您可以使用:
/* Retreiving the root domain attributes
*/
sFromWhere = "LDAP://DC_DNS_NAME:389/dc=dom,dc=fr";
DirectoryEntry deBase = new DirectoryEntry(sFromWhere, "AdminLogin", "PWD");
DirectorySearcher dsLookForDomain = new DirectorySearcher(deBase);
dsLookForDomain.Filter = "(objectClass=*)";
dsLookForDomain.SearchScope = SearchScope.base;
dsLookForDomain.PropertiesToLoad.Add("msDS-PrincipalName");
SearchResult srcDomains = dsLookForDomain.FindOne();
這篇關于UserPrincipal 對象中的域名在哪里?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!