2016-04-25 62 views
1

我有以下代码返回给我一个UserPrincipal但登录名永远不会包含域名。也没有“域名”或类似的属性。如何从UserPrincipal或PrincipalSearcher获取域名

我怎样才能从UserPrincipal或PrincipalSearcher的域到用户/返回的用户?

PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 
    UserPrincipal user = new UserPrincipal(ctx); 
    user.SamAccountName = txtSearch.Text; 
    PrincipalSearcher searcher = new PrincipalSearcher(user); 

    PrincipalSearchResult<Principal> results = searcher.FindAll(); 
    foreach (UserPrincipal u in results) 
    { 
     Response.Write(u.Name + "<br />"); 
    } 
+0

这个问题:http://stackoverflow.com/questions/10702188/where-is-the-domain-name-in-a-userprincipal-object是类似的,但没有接受的答案这一个:http:// stackoverflow.com/questions/4284641/get-netbiosname-from-a-userprincipal-object –

回答

3

它为我用

usercontext.Sid.Translate(typeof(NTAccount)).ToString(); 

这将返回domain\user