ASP.NET – Get User Name Under Windows Authentication
by Byron Bennett / March 3rd, 2009
Getting the authenticated user’s name when using Windows Authentication with ASP.NET is very simple. I just keep having to go back to Google or dig out an old project to remember it, so here it is:
string username = User.Identity.Name.ToString();
Be sure to add the Security reference:
using System.Web.Security;
Thats it!
[UPDATE] Check out the comments below for some additional suggestions on getting the user name without the Domain name.
Byron
Popularity: 37%
Filed under: ASP.NET/C#

June 26th, 2009 at 12:18 pm
Thanks for the help above though
The above code returns the whole name including the computer name
eg
when i write those codes to my computer i gives me the following result
BROMPO-379477BA\Brian
But i just want to get Brian and not the whole string how will i do that???
June 28th, 2009 at 12:20 am
Brain,
I’m not sure. I was getting the domain name when I was using it since I was on a LAN with Active Directory running. Since the domain was always the same for my app, I checked for it and replaced it. Sorry I don’t have the answer right now.
BB
July 3rd, 2009 at 3:15 am
Hi Brian,
You try with below code
string strAccount = HttpContext.Current.User.Identity.Name.ToString();
string strUserName = strAccount .Split(“\\”.ToCharArray())[1];
have fun coding,
BB
July 9th, 2009 at 1:24 am
DirectoryEntry dEntry = new DirectoryEntry(“WinNT://” + Environment.UserDomainName + “/” + Environment.UserName);
return dEntry.Properties["fullName"].Value.ToString();
July 9th, 2009 at 7:24 am
@Thang, @AD Guy,
Thanks for the hints!
Byron
December 14th, 2009 at 3:05 pm
Even easier to get the AD user name
my.user.name