Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the current windows user's name in username@domain format?

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.

like image 779
Tony Vitabile Avatar asked Aug 28 '12 19:08

Tony Vitabile


People also ask

What format is domain username?

Also known as the domain name. A UPN can be implicitly or explicitly defined. An implicit UPN is of the form [email protected].


5 Answers

Something like this should work...

string[] temp = Convert.ToString(WindowsIdentity.GetCurrent().Name).Split('\\');
string userName = temp[1] + "@" + temp[0];
like image 104
Kevin Avatar answered Oct 22 '22 04:10

Kevin


var input =  WindowsIdentity.GetCurrent().Name ;
string[] tab = input.Split('\\');
var result = tab[1] + "@" +  tab[0];
like image 37
Aghilas Yakoub Avatar answered Oct 22 '22 03:10

Aghilas Yakoub


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.

like image 23
Tony Vitabile Avatar answered Oct 22 '22 04:10

Tony Vitabile


Use

System.DirectoryServices.AccountManagement.UserPrincipal.Current.UserPrincipalName

This returns the UPN of the current user. Requires a reference to System.DirectoryServices.AccountManagement.

like image 30
Simon Giles Avatar answered Oct 22 '22 05:10

Simon Giles


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.

like image 30
StoriKnow Avatar answered Oct 22 '22 03:10

StoriKnow