|
-
December 3rd, 2008, 09:58 AM
#1
WindowsIdentity in C#
Hello Everyone,
I have a question regarding windows Identity.
I am using the following code to get the user name of the user which is currently logged in the system
Code:
string pcLoggedInUserName, loggedInuserWithoutDomain = string.Empty;
int indexOfFirstSlash;
pcLoggedInUserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
indexOfFirstSlash = pcLoggedInUserName.IndexOf('\\');
loggedInuserWithoutDomain = pcLoggedInUserName.Substring(indexOfFirstSlash + 1);
return loggedInuserWithoutDomain;
As you can see I need to get the user name only without the domain name. Right now the WindowsIdentity.GetCurrent().Name returns DOMAIN\\username and I need to get the user name only. I have managed to accomplish that by getting the substring.
My question is this: Does the GetCurrent() function always return the username in the DOMAIN\\username format ( since my logic depends on that) or is it system speciffic?
Thanks in advance
Susan
-
December 3rd, 2008, 10:07 AM
#2
Re: WindowsIdentity in C#
To get the username you could simply use System.Environment.UserName.
-
December 3rd, 2008, 11:16 AM
#3
Re: WindowsIdentity in C#
Thanks so much for the fast reply. It has been very helpful.
I implemented the suggestion and my solution looks much cleaner.
Regards,
Susan
-
December 4th, 2008, 08:17 AM
#4
Re: WindowsIdentity in C#
As far as I know, domain name is part of windows identity, so it is allways listed together with the plain user name.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|