Given the user SID, how do I get their userPrincipalName?

I have a list of user SIDs and I need to get a list of userPrincipalName ... is there a way I can get it without loading DirectoryEntry users and pulling in the userPrincipalName property?

I need the most efficient method because it has been done a lot.

+6
c # active-directory directoryservices
source share
2 answers

If you are using .NET 3.5, check out this great MSDN article, "Managing Directory Security Principles," in the .NET Framework 3.5 .

It features new advanced search capabilities for the .NET 3.5 System.DirectoryServices.AccountManagement .

One nice feature is the FindByIdentity method, which allows you to find the user (or group) based on the identifier - whether it is the user principal name, distinguished name, GUID or SID - it will just work:

 UserPrincipal user = UserPrincipal.FindByIdentity(principalContext, IdentityType.Sid, (value)); 

You need to make sure that the SID is in the correct format - see MSDN docs for details.

Once you have the main user object, simply enter its user principal name:

 if(user != null) { string upn = user.UserPrincipalName; } 

The sample code for the article even has two additional helper methods FindByIdentityGuid and FindByIdentitySid to achieve exactly what you are looking for!

Go to it and use it.

+6
source share

You can get this using the LookupAccountSid () method to call Win32. On the page I'm linked to, there is sample code that shows a simple example.

-one
source share

All Articles