Environment.UserName returning application pool name instead of username
Asked Answered
A

4

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 ?

Ascites answered 14/1, 2014 at 10:50 Comment(0)
H
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.

Halfassed answered 14/1, 2014 at 10:52 Comment(4)
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 identitiesAscites
This should work with Windows authentication. Have a look here: msdn.microsoft.com/en-us/library/…Halfassed
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 ?Ascites
configure IIS to enable integrated securityHalfassed
L
2

Try using

Request.ServerVariables["LOGON_USER"]

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

Luxuriance answered 14/1, 2014 at 10:52 Comment(0)
K
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/

Krystynakshatriya answered 9/4, 2018 at 13:28 Comment(2)
Environment.UserName, returns name if the user who started the current thread but Environment.GetEnvironmentVariable("USERNAME") returns you login user name.Krystynakshatriya
Done lot of research and finally this worked with me.Krystynakshatriya
C
1

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

Counterstroke answered 10/7, 2020 at 18:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.