[英]Environment.UserName returning application pool name instead of username
The following line以下行
Environment.UserName
In debug mode in visual studio returns the identity of the user like I need.在 Visual Studio 的调试模式下,返回我需要的用户身份。
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.然后,当我在 IIS 中设置我的站点并运行代码时,同一行返回该站点使用的应用程序池的名称。
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.重要说明:您需要配置 IIS 以启用集成安全并禁用匿名登录。
Note that Environment.Username
returns the Username on the current Thread.请注意,
Environment.Username
返回当前线程上的用户名。
Try using尝试使用
Request.ServerVariables["LOGON_USER"]
It will return DOMAIN\\USERNAME
.它将返回
DOMAIN\\USERNAME
。 You can then split it etc.然后你可以拆分它等等。
This worked for me.这对我有用。 Use Environment.GetEnvironmentVariable("USERNAME") for current Login username.
使用 Environment.GetEnvironmentVariable("USERNAME") 作为当前登录用户名。
Link : https://www.c-sharpcorner.com/uploadfile/puranindia/the-environment-class-in-C-Sharp/链接: https : //www.c-sharpcorner.com/uploadfile/puranindia/the-environment-class-in-C-Sharp/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.