How to get current Windows username in username @domain format?

I know that the following function returns the current Windows username in the format domain \ username.

Convert.ToString( WindowsIdentity.GetCurrent().Name ); 

But how do I get a username in the format username@domain ?

EDIT:

I am responding to this edit as everyone who answered has the same basic idea.

From what I was given to understand, parsing a name from the domain\username format and creating it as username@domain unsafe or not recommended. I believe this is because there is no guarantee that the two domain names are the same in different formats. For example, in the company where I work, part of the domain format domain\username based on cancellation, but in username@domain this is the name of the company. This is what requires a DNS lookup.

I was hoping there was an API that performed this DNS lookup. I think I should have put this information in my original question. Unfortunately.

+8
security c # windows active-directory windows-identity
source share
6 answers

All code for entering a name in the format Domain\user name and its analysis will not work in all situations. The answer is that you must make calls in Active Directory to get the user principal name. It turns out that I can not rely on the Active Directory installed on the desktop, since many police units do not install the directory on their laptops if it is stolen and the police officer is not in the car. (Talk about the merciless, theft of a computer from a police car!)

We came up with our own solution for our situation. We store user names in our database in the format Domain\user name . When the program starts, it checks to see if the current Windows username (in the same format) is in the database. If so, the program uses this user as the current user and starts. If the current Windows user is not in our database, the program returns to our previous code.

Thus, the user can log in using any format for his username and authenticate with Windows. Our program always gets the username in the same format and always checks the username in this format. Windows authenticates the user, not us.

+3
source share

Something like this should work ...

 string[] temp = Convert.ToString(WindowsIdentity.GetCurrent().Name).Split('\\'); string userName = temp[1] + "@" + temp[0]; 
+9
source share

You can split the name using \ as a delimiter, and then reverse the order as follows:

 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 is important to note that a double backslash \\ is required because the backslash is a special character. We add a second backslash in the example above to avoid the special character and use it as a regular character.

+3
source share
 var input = WindowsIdentity.GetCurrent().Name ; string[] tab = input.Split('\\'); var result = tab[1] + "@" + tab[0]; 
+3
source share

Use

 System.DirectoryServices.AccountManagement.UserPrincipal.Current.UserPrincipalName 

This returns the UPN of the current user. Link to System.DirectoryServices.AccountManagement required.

+2
source share

Something like that.

 var nameParts = WindowsIdentity.GetCurrent().Name.Split(@"\"); string name = nameParts.Length == 1 ? nameParts : string.format("{0}@{1}",nameParts[1],nameParts[0]); 
0
source share

All Articles