=> dimasite

IT for(;;) fun!

How to get current windows user name using ASP .NET

Posted by Dmitrij Ivanov on September 21, 2010

There are three different ways to obtain the same information.
protected void Page_Load(object sender, EventArgs e)
{
  System.Security.Principal.WindowsPrincipal windowsPrincipal = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal;
  string identityName1way = windowsPrincipal.Identity.Name;
  string identityName2way = HttpContext.Current.User.Identity.Name;
  string identityName3way = Request.ServerVariables["AUTH_USER"];
  Label1.Text = identityName1way + " or " + identityName2way + " or " + identityName3way;
}

The Web.config file should have authentication mode set to Windows.
<configuration>
  <system.web>
     <authentication mode="Windows" />
     ...
  </system.web>
</configuration>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: