[英]Environment.UserName returning as SYSTEM rather than User's name
[英]Environment.UserName returning application pool name instead of username
以下行
Environment.UserName
在 Visual Studio 的調試模式下,返回我需要的用戶身份。
然后,當我在 IIS 中設置我的站點並運行代碼時,同一行返回該站點使用的應用程序池的名稱。
我怎樣才能讓它仍然返回用戶名?
嘗試這樣的事情:
if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
string username = System.Web.HttpContext.Current.User.Identity.Name;
}
重要說明:您需要配置 IIS 以啟用集成安全並禁用匿名登錄。
請注意, Environment.Username
返回當前線程上的用戶名。
嘗試使用
Request.ServerVariables["LOGON_USER"]
它將返回DOMAIN\\USERNAME
。 然后你可以拆分它等等。
這對我有用。 使用 Environment.GetEnvironmentVariable("USERNAME") 作為當前登錄用戶名。
鏈接: https : //www.c-sharpcorner.com/uploadfile/puranindia/the-environment-class-in-C-Sharp/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.