How to access Windows user information in C#

When your Web project is based on Windows authentication it does not require a login page. When end user requests a Web page that requires authentication, his browser transmits the credential to IIS and your application than retrieves information from the User property of the web page. You can use the next subroutine to display the currently authenticated user:

 

if (Request.IsAuthenticated)

{

// Display generic identity information.

lblInfoText.Text = “<b>Name: </b>” + User.Identity.Name;

lblInfoText.Text += “<br><b>Authenticated With: </b>”;

lblInfoText.Text += User.Identity.AuthenticationType;

}

 

Note: In case of Windows authentication the user name is always in the form DomainName\UserName or ComputerName\UserName and the next picture shows the result:

 

Displaying user information in C#

Displaying user information in C#