简体   繁体   English

Environment.UserName 返回应用程序池名称而不是用户名

[英]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/

In IIS, for your application, please .在 IIS 中,对于您的应用程序,请 . Enable ASP.NET Impersonation启用 ASP.NET 模拟在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Environment.UserName 作为 SYSTEM 而不是用户名返回 - Environment.UserName returning as SYSTEM rather than User's name 我可以使用 Environment.Username 和 MYSQL 来验证登录吗? - Can I use Environment.Username and MYSQL to verify log in? 在 Windows 服务中使用时 Environment.UserName 会返回什么 - What would Environment.UserName return when used in a Windows Service 将app.config值设置为Environment.UserName - Set app.config value to Environment.UserName Environment.UserName会在Windows 7,Windows 8.x和Windows 10中返回相同的结果吗? - Will Environment.UserName return the same result in Windows 7, Windows 8.x and Windows 10? Environment.UserName 为同一用户(外壳)提供不同的结果:替代/转换? - Environment.UserName gives different results for same user (casing): alternative/transformation? 名称声明返回用户名而不是名称 - Name claim returning username and not name 在LoginName控件中显示全名而不是用户名 - Display Full Name Instead of Username in LoginName Control 如何在MVC中使用User.Identity.Name显示名称而不是UserName - How to display Name instead of UserName with User.Identity.Name in MVC .NET返回不正确的用户名 - .NET returning incorrect username
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM