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

问题描述 投票:7回答:3

以下行

Environment.UserName

在Visual Studio的调试模式中,返回用户身份,就像我需要的那样。

然后,当我在IIS中设置我的站点并运行代码时,同一行返回该站点使用的应用程序池的名称。

如何让它仍然返回用户名?

c# asp.net .net iis user-identification
3个回答
5
投票

尝试这样的事情:

if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
   string username = System.Web.HttpContext.Current.User.Identity.Name;
}

重要说明:您需要配置IIS以启用集成安全性并禁用匿名登录。

请注意,Environment.Username返回当前线程上的用户名。


2
投票

尝试使用

Request.ServerVariables["LOGON_USER"]

它将返回DOMAIN\USERNAME。然后你可以拆分它等。


0
投票

这对我有用。使用Environment.GetEnvironmentVariable(“USERNAME”)获取当前登录用户名。

链接:https://www.c-sharpcorner.com/uploadfile/puranindia/the-environment-class-in-C-Sharp/

© www.soinside.com 2019 - 2024. All rights reserved.