Reputation: 8614
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.
Upvotes: 11
Views: 42156
Reputation: 27017
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;
}
Upvotes: 0
Reputation: 796
Use
System.DirectoryServices.AccountManagement.UserPrincipal.Current.UserPrincipalName
This returns the UPN of the current user. Requires a reference to System.DirectoryServices.AccountManagement.
Upvotes: 4
Reputation: 167
Something along these lines.
var nameParts = WindowsIdentity.GetCurrent().Name.Split(@"\");
string name = nameParts.Length == 1
? nameParts
: string.format("{0}@{1}",nameParts[1],nameParts[0]);
Upvotes: 0
Reputation: 8614
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.
Upvotes: 5
Reputation: 5866
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.
Upvotes: 4
Reputation: 704
Something like this should work...
string[] temp = Convert.ToString(WindowsIdentity.GetCurrent().Name).Split('\\');
string userName = temp[1] + "@" + temp[0];
Upvotes: 13
Reputation: 28990
var input = WindowsIdentity.GetCurrent().Name ;
string[] tab = input.Split('\\');
var result = tab[1] + "@" + tab[0];
Upvotes: 4