Environment.UserName returning application pool name instead of username

断了今生、忘了曾经 提交于 2020-01-22 19:26:40

问题


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 ?


回答1:


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.




回答2:


Try using

Request.ServerVariables["LOGON_USER"]

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




回答3:


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/



来源:https://stackoverflow.com/questions/21111687/environment-username-returning-application-pool-name-instead-of-username

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!