Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the domain name in a UserPrincipal object?

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();

I want to get the domain-qualified username in the "friendly" (aka. "pre-Windows 2000" format), eg. "CONTOSO\SmithJ". 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.

like image 894
EMP Avatar asked May 22 '12 12:05

EMP


People also ask

Where can I find user UPN?

You might not know your UPN, and you might not be a domain admin. To find out the UPN for your account, run the following command from your workstation: whoami /upn. Although the result looks like an email address, it's the UPN on your local domain account.

What is UPN name in Active Directory?

In the Windows operating system's Active Directory, a User Principal Name (UPN) is the name of a system user in an e-mail address format. The user name (or "username") is followed by the "at sign" followed by the name of the Internet domain with which the user is associated.

What is UPN format?

User principal name (UPN) format is used to specify an Internet-style name, such as [email protected].

How do I update Active Directory in UPN?

Changing the User Principal Name (UPN) in Active Directory The easiest way to do it is to change UserPrincipalName in user properties in the ADUC console ( dsa. msc ). As you can see, all UPN suffixes of the domain are available in the list. Select the one you want and click OK.


1 Answers

For AD DS, the value of msDS-PrincipalName is the NetBIOS domain name, followed by a backslash ("\").

You can find it using :

/* 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();
like image 188
JPBlanc Avatar answered Nov 16 '22 03:11

JPBlanc