The WindowsIdentity(string)
constructor requires the username to be in username@domain.com
format. But in my case I get the usernames from a DB in the old DOMAIN\user
format (and then have to check their Windows role membership).
What is the best way of creating WindowsPrincipal
from the old style (sAMAccountName) username?
It does seem that there is no way of converting the username format without involving a query to Active Directory. Since that is the case there is no need to create
WindowsPrincipal
for checking the group membership since that would probably need yet another connection to AD.By using the
System.DirectoryServices.AccountManagement
namespace you can both get the UPN of the user and check the group membership.I tooked the DsCrackNames on example pinvoke.net and modified it to convert from nt4 name to UPN. Its kinda sloppy and you might want to clean up. For this it has to hit the DS too. They have the DS_NAME_FLAG_SYNTACTICAL_ONLY flag which can be used to not hit the directory but I dont think that will work here.
You could query AD for the UPN.
This works fine but involves a query to active directory/SAM store (depending on the context)...