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.
marc_s
source share