How to get the Windows User Name of the logged in user in ASP.Net, IIS

Hello There!

So, today i’ll speak  about how you can get the User ID of the current user who has logged into the Web Application.

Lets consider the Web Application is hosted in IIS.

Here, I am avoiding all the boiler plate code to create and set up your project and host into the IIS.

There are three ways to get the logged in User ID in ASP.Net.

FIRST WAY

<authentication mode=”Windows”/>

You can add the authentication tag into the <system.web> tag in the Web.config file of your project.

Enable the Anonymous access in IIS. You will find this setting in your Application under the Authentication Option.

Authentication.png EnableAnonymousAuthentication.png

Write the below lines of code in your application CS or VB file.

  1. System.Environment.UserName .. Will give you the Computer Name
  2. Page.User.Identity.Name .. Will give you Blank
  3. System.Security.Principal.WindowsIdentity.GetCurrent().Name .. Will give you the Computer Name

SECOND WAY

<authentication mode=”Windows”/>

You can add the authentication tag into the <system.web> tag in the Web.config file of your project.

Disable the Anonymous access & Enable Windows Authentication in IIS. You will find this setting in your Application under the Authentication Option.

Authentication.pngEnableWindowsAuthentication.png

After setting the Application to enable Windows Authentication, you will have to migrate your application to a Classic .NET AppPool and enable the  Integrated Pipeline for the Classic .Net AppPool inside the IIS.

AppPool.png

Write the below lines of code in your application CS or VB file.

  1. System.Environment.UserName .. Will give you ASPNET. i.e the account that is used to run the Asp.Net service.
  2. Page.User.Identity.Name .. Will give you Domain\Windows Account Name
  3. System.Security.Principal.WindowsIdentity.GetCurrent().Name .. Will give you Computer Name\ASPNET

 

THIRD WAY

<authentication mode=”Windows”/> <identity impersonate =”true”/>

You can add the above tags into the <system.web> tag in the Web.config file of your project.

Disable the Anonymous access & Enable Windows Authentication in IIS

Authentication.pngEnableWindowsAuthentication.png

After setting the Application to enable Windows Authentication, you will have to migrate your application to a Classic .NET AppPool and enable the  Integrated Pipeline for the Classic .Net AppPool inside the IIS.

AppPool.png

Write the below lines of code in your application CS or VB file.

  1. System.Environment.UserName .. Will give you Windows Account Name
  2. Page.User.Identity.Name .. Will give you Domain\Windows Account Name
  3. System.Security.Principal.WindowsIdentity.GetCurrent().Name .. Will give you Domain\Windows Account Name

* Browser will ask you for the user name and password if the Windows Authentication is enabled.

That’s all you have to do to fetch the Windows User ID in ASP .Net IIS environment.

Cheers!

Rohant K.

 

Advertisements

One thought on “How to get the Windows User Name of the logged in user in ASP.Net, IIS

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