I know that the following function returns the current Windows user's name in domain\username format.
Convert.ToString( WindowsIdentity.GetCurrent().Name );
But how do I obtain the user's name in username@domain
format?
EDIT:
I'm responding in this edit as everyone who has replied has the same basic idea.
From what I've been given to understand, parsing the name from domain\username
format and constructing it as username@domain
is not safe or advised. I believe this is so because there is no guarantee that the two domain names are the same in the different formats. For example, in the company where I work, the domain
part of the domain\username
format is based upon deparment, but in the username@domain
, it's the company name. It's the kind of thing that requires a DNS lookup.
I was hoping that there was an API that did this DNS lookup. I guess I should have put this information into my original question. Sorry.
Also known as the domain name. A UPN can be implicitly or explicitly defined. An implicit UPN is of the form [email protected].
Something like this should work...
string[] temp = Convert.ToString(WindowsIdentity.GetCurrent().Name).Split('\\');
string userName = temp[1] + "@" + temp[0];
var input = WindowsIdentity.GetCurrent().Name ;
string[] tab = input.Split('\\');
var result = tab[1] + "@" + tab[0];
All of the code to take the name in Domain\user name
format and parse it will not work in all situations. The answer is you have to make calls to Active Directory to get the User Principal Name. It turns out that I can't rely on Active Directory being installed on the desktop, since many police departments don't install the directory on their laptops in case it is stolen while a cop is not in the car. (Talk about gutsy, stealing a computer out of a police vehicle!)
We have come up with our own solution for our situation. We store the user names in our database in Domain\user name
format. When the program starts up, it checks to see if the current windows user name (in the same format) is in the database. If it is, the program uses that user as the current user and runs. If the current Windows user is not in our database, the program falls back to our previous code.
This way, the user can log into the machine using any format for their user name and they authenticate with Windows. Our program always gets the user name in the same format and it always checks the user name in that format. Windows authenticates the user and not us.
Use
System.DirectoryServices.AccountManagement.UserPrincipal.Current.UserPrincipalName
This returns the UPN of the current user. Requires a reference to System.DirectoryServices.AccountManagement.
You could split the name using \
as the delimiter, then reverse the order like so:
string[] splitName = WindowsIdentity.GetCurrent().Name.Split('\\');
//check that splitName contains at least 2 values before using
string name = (splitName.Length > 1) ? splitName[1] + "@" + splitName[0] : null;
It's important to note that a double backslash \\
is required because a backslash is a special character. We add the second backslash in the above example to escape the special character and use it as a regular character.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With