Search code examples
c#asp.netiisenvironment-variablesvisual-studio-debugging

IIS returns different USERNAME than debug


I have a C# web application that calls Environment.GetEnvironmentVariable("USERNAME"). When debugging it returns the username I am signed in to the machine as it should.

I then set up my site in IIS. The site runs Anonymous and Forms Authentication. The site also runs on DefaultAppPool, but the identity is a service account that has proper access to the DB. When I hit a page that has Environment.GetEnvironmentVariable("USERNAME") the returned value is the name of the server. The returned value is neither the AD user I am logged in as nor the service account. Why is it pulling the server name as the USERNAME?


Solution

  • @LexLi provided the line of code needed to pull the Windows context the user's browser is hitting the website with. Page.User