简体   繁体   中英

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 );

Something like this should work...

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

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.

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.

Something along these lines.

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

I've seen so many variations of the same, why not put it into a function like:

string GetUserName()
{
    var currentName =  WindowsIdentity.GetCurrent()?.Name;
    if (string.IsNullOrEmpty(currentName)) return null;
    var nameSplit = currentName.Split(@"\");
    string name = (nameSplit.Length > 1)
                    ? $"{nameSplit[1]}@{nameSplit[0]}" : currentName;
    return name;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM