如何获取正在访问ASP.NET应用程序的当前用户?

38

为了获取当前已登录的用户,我使用以下代码:

string opl = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

我在一个ASP.NET应用程序上工作,需要这些信息。因此,我将我的应用程序放在服务器上并尝试了上面的代码,然后我得到了字符串opl中的“Network Service”。我需要知道访问我的ASP.NET应用程序的PC的当前用户。


请参见https://dev59.com/o4bca4cB1Zd3GeqPTDgH,以获取有关ASP.NET如何获取Windows登录用户名的更多信息。 - Lucas Dal Piaz
8个回答

60

10
如果您正在寻找用户登录信息,请查找特定的字符串System.Web.HttpContext.Current.User.Identity.Name - Nashwan
1
阅读这篇文章,您需要配置Windows身份验证。 - BenCr
你的IIS配置正确吗?你是否处于内部网络场景中,凭据可以“流动”到服务器上。Windows身份验证在更广泛的互联网上不起作用。 - BenCr
看一下这个相关的问题,你可以尝试一些那里的建议。https://dev59.com/tEjSa4cB1Zd3GeqPHrIB - BenCr
如果匿名身份验证也启用,则使用它而不是Windows身份验证。 - Karlth
显示剩余2条评论

25
使用System.Web.HttpContext.Current.User.Identity.Name应该可以工作。 请按以下步骤检查托管您的站点的服务器上的IIS网站设置:
  1. 进入 IIS → Sites → 您的站点 → Authentication

    IIS设置

  2. 现在检查是否禁用了匿名访问并启用了Windows身份验证

    身份验证

  3. 现在,System.Web.HttpContext.Current.User.Identity.Name应返回类似于此的内容:

    域名\用户名


19
如果你正在使用Membership,可以这样做:Membership.GetUser() 你的代码返回的是分配给ASP.NET的Windows账户。
额外信息编辑: 你需要包含System.Web.Security。
using System.Web.Security

1
我可以在哪个命名空间中找到它? - Tassisto
2
System.Web.Security 命名空间 - Robert
我得到了一个错误:"对象引用未设置为对象的实例。" - Tassisto
你使用 ASP .NET 的成员特性吗?你使用哪种身份验证模式? - Robert
1
Windows身份验证,我怎样才能决定使用哪个功能? - Tassisto
显示剩余2条评论

11
最佳实践是先检查Identity.IsAuthenticated属性,然后像这样获取usr.UserName:
string userName = string.Empty;

if (System.Web.HttpContext.Current != null && 
    System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
    System.Web.Security.MembershipUser usr = Membership.GetUser();
    if (usr != null)
    {  
        userName = usr.UserName;
    }
}

7
您可以直接使用页面的属性。有趣的是,您可以在代码的任何地方访问该属性。
使用如下代码:
HttpContext.Current.User.Identity.Name

3

不要想得太远。

如果你使用ASP.NET MVC开发,用户只是Controller类的一个属性(property of the Controller class)。因此,如果你在一些模型中迷失了寻找当前用户的信息,请尝试退回到控制器中获取相关信息。

在控制器中,只需使用:

using Microsoft.AspNet.Identity;

  ...

  var userId = User.Identity.GetUserId();
  ...

使用字符串作为 userId


1
上面的普遍共识答案似乎存在CORS支持的兼容性问题。为了使用HttpContext.Current.User.Identity.Name属性,您必须禁用匿名身份验证,以强制Windows身份验证提供经过身份验证的用户信息。不幸的是,我认为在CORS场景中,您必须启用匿名身份验证才能处理预检OPTIONS请求。
您可以通过保留匿名身份验证并改用HttpContext.Current.Request.LogonUserIdentity属性来解决此问题。即使启用了匿名身份验证,这将返回经过身份验证的用户信息(假设您处于内部网络场景中)。该属性返回一个WindowsUser数据结构,两者均定义在System.Web命名空间中。
        using System.Web;
        WindowsIdentity user;
        user  = HttpContext.Current.Request.LogonUserIdentity;

1

我遇到了同样的问题。

这是对我有用的解决方法:

Setting up Authentication in IIS Panel

Setting up Properties of Windows Authentication in IIS

在IIS中设置Windows身份验证的属性 NTLM必须是最上面的

NTLM必须是最上面的。 进一步的Web.config修改,请确保您已经拥有或添加了以下内容(如果不存在):

<system.web>

  <authentication mode="Windows" />
  <identity impersonate="true"/>

</system.web>

 <!-- you need the following lines of code to bypass errors, concerning type of Application Pool (integrated pipeline or classic) -->

<system.webServer>
   <validation validateIntegratedModeConfiguration="false"/>
</system.webServer>

请看下面对节点 <system.web> 和 <system.webServer> 的合法解释:

<system.web>和<system.webServer>之间的区别是什么?

当然,你可以通过以下方式获取用户名:

//I am using the following to get the index of the separator "\\" and remove the Domain name from the string
int indexOfSlashChar = HttpContext.Current.User.Identity.Name.IndexOf("\\"); 

loggedInWindowsUserName = HttpContext.Current.User.Identity.Name.Substring(indexOfSlashChar + 1);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接