Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment.UserName returning application pool name instead of username

The following line

Environment.UserName

In debug mode in visual studio returns the identity of the user like I need.

Then when I set up my site in IIS and run the code the same line returns the name of the application pool which the site uses.

How can I get it to still return the user name ?

like image 326
StevieB Avatar asked Jan 14 '14 10:01

StevieB


1 Answers

Try something like this:

if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
   string username = System.Web.HttpContext.Current.User.Identity.Name;
}

Important note: You need to configure IIS to enable integrated security and disable anonymous logon.

Note that Environment.Username returns the Username on the current Thread.

like image 200
Matt Wilko Avatar answered Oct 14 '22 07:10

Matt Wilko