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 ?

有帮助吗?

解决方案

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.

其他提示

Try using

Request.ServerVariables["LOGON_USER"]

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

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/

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top