7

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 ?

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
StevieB
  • 6,263
  • 38
  • 108
  • 193

4 Answers4

5

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.

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • So there is no login to this application at the moment, it's just something for local users on the network. I just wanted to be able to capture user identities – StevieB Jan 14 '14 at 10:58
  • 1
    This should work with Windows authentication. Have a look here: http://msdn.microsoft.com/en-us/library/system.web.httpcontext.user(v=vs.110).aspx – Matt Wilko Jan 14 '14 at 11:08
  • Ok so right now it's not getting into the if loop because basically there is no Authentication set up. How do I set up Windows Authentication on the app ? – StevieB Jan 14 '14 at 11:20
  • configure IIS to enable integrated security – Matt Wilko Jan 14 '14 at 11:24
2

Try using

Request.ServerVariables["LOGON_USER"]

It will return DOMAIN\USERNAME. You can then split it etc.

ttaaoossuuuu
  • 7,786
  • 3
  • 28
  • 58
2

This worked for me. Use Environment.GetEnvironmentVariable("USERNAME") for current Login username.

Link :https://www.c-sharpcorner.com/uploadfile/puranindia/the-environment-class-in-C-Sharp/

1

In IIS, for your application, please . Enable ASP.NET Impersonation enter image description here

IamP
  • 108
  • 7