274

在表单模型中,我曾经通过以下方式获取当前登录用户:

Page.CurrentUser

如何在 ASP.NET MVC 的控制器类中获取当前用户?

4

21 回答 21

271

如果您需要从控制器中获取用户,请使用UserController 的属性。如果您从视图中需要它,我会在 中填充您特别需要的内容ViewData,或者您​​可以只调用 User ,因为我认为它是ViewPage.

于 2008-11-04T23:19:36.857 回答
207

我发现这User行得通,也就是说,User.Identity.Name或者User.IsInRole("Administrator")

于 2009-05-27T15:02:05.350 回答
62

试试HttpContext.Current.User

公共共享属性 Current() 作为 System.Web.HttpContext
的 System.Web.HttpContext 成员

摘要:
获取或设置当前 HTTP 请求的 System.Web.HttpContext 对象。

返回值:
当前 HTTP 请求的 System.Web.HttpContext

于 2008-11-04T21:30:22.520 回答
39

您可以像这样在 ASP.NET MVC4 中获取用户的名称:

System.Web.HttpContext.Current.User.Identity.Name
于 2013-09-22T19:38:40.363 回答
21

我意识到这真的很老了,但我刚刚开始使用 ASP.NET MVC,所以我想我会坚持我的两分钱:

  • Request.IsAuthenticated告诉您用户是否已通过身份验证。
  • Page.User.Identity为您提供登录用户的身份。
于 2009-03-25T01:42:00.003 回答
16

我用:

Membership.GetUser().UserName

我不确定这是否适用于 ASP.NET MVC,但值得一试:)

于 2008-11-04T21:29:28.497 回答
11

登录用户名:System.Web.HttpContext.Current.User.Identity.Name

于 2010-09-08T12:50:12.053 回答
10

用户名:

User.Identity.Name

但是,如果您只需要获取 ID,则可以使用:

using Microsoft.AspNet.Identity;

因此,您可以直接获取用户 ID:

User.Identity.GetUserId();
于 2017-01-12T13:44:53.410 回答
10

我们可以使用以下代码在 ASP.Net MVC 中获取当前登录的用户:

var user= System.Web.HttpContext.Current.User.Identity.GetUserName();

var userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; //will give 'Domain//UserName'

Environment.UserName - Will Display format : 'Username'
于 2018-01-23T23:28:23.173 回答
9

为了引用使用控制器中内置于 ASP.NET MVC 4 中的简单身份验证创建的用户 ID 以进行过滤(如果您首先使用数据库和 Entity Framework 5 生成代码优先绑定并且您的表的结构如此,这将很有帮助使用了用户 ID 的外键),您可以使用

WebSecurity.CurrentUserId

一旦添加了 using 语句

using System.Web.Security;
于 2013-06-22T19:12:38.247 回答
6

此页面可能是您要查找的内容:
在 MVC3 中使用 Page.User.Identity.Name

你只需要User.Identity.Name.

于 2011-11-13T08:51:33.567 回答
5

使用System.Security.Principal.WindowsIdentity.GetCurrent().Name.

这将获取当前登录的 Windows 用户。

于 2015-08-17T18:04:02.767 回答
4

对于它的价值,在 ASP.NET MVC 3 中,您可以只使用返回当前请求的用户的用户。

于 2011-09-13T19:10:35.230 回答
4

如果您在登录页面内,例如在 LoginUser_LoggedIn 事件中,Current.User.Identity.Name 将返回一个空值,因此您必须使用 yourLoginControlName.UserName 属性。

MembershipUser u = Membership.GetUser(LoginUser.UserName);
于 2011-11-10T13:44:24.513 回答
4

您可以使用以下代码:

Request.LogonUserIdentity.Name;
于 2017-08-14T11:42:13.240 回答
3
IPrincipal currentUser = HttpContext.Current.User;
bool writeEnable = currentUser.IsInRole("Administrator") ||
        ...
                   currentUser.IsInRole("Operator");
于 2011-04-26T13:00:09.497 回答
2
var ticket = FormsAuthentication.Decrypt(
                    HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName].Value);

if (ticket.Expired)
{
    throw new InvalidOperationException("Ticket expired.");
}

IPrincipal user =  (System.Security.Principal.IPrincipal) new RolePrincipal(new FormsIdentity(ticket));
于 2015-06-24T07:47:06.033 回答
2

如果您恰好在 Intranet 上的 Active Directory 中工作,这里有一些提示:

(Windows 服务器 2012)

在 Web 服务器上运行任何与 AD 对话的东西都需要大量的更改和耐心。由于在 Web 服务器与本地 IIS/IIS Express 上运行时,它以 AppPool 的身份运行,因此,您必须将其设置为模拟访问该站点的任何人。

当您的 ASP.NET MVC 应用程序在网络内的 Web 服务器上运行时,如何在活动目录中获取当前登录用户:

// Find currently logged in user
UserPrincipal adUser = null;
using (HostingEnvironment.Impersonate())
{
    var userContext = System.Web.HttpContext.Current.User.Identity;
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain, ConfigurationManager.AppSettings["AllowedDomain"], null,
                                                ContextOptions.Negotiate | ContextOptions.SecureSocketLayer);
    adUser = UserPrincipal.FindByIdentity(ctx, userContext.Name);
}
//Then work with 'adUser' from here...

您必须将与“活动目录上下文”有关的所有调用包装在以下内容中,以便它充当获取 AD 信息的托管环境:

using (HostingEnvironment.Impersonate()){ ... }

您还必须impersonate在 web.config 中设置为 true:

<system.web>
    <identity impersonate="true" />

您必须在 web.config 中启用 Windows 身份验证:

<authentication mode="Windows" />
于 2016-01-22T16:19:31.980 回答
2

在 Asp.net Mvc Identity 2 中,可以通过以下方式获取当前用户名:

var username = System.Web.HttpContext.Current.User.Identity.Name;
于 2017-09-29T14:18:46.807 回答
1

在 IIS 管理器的身份验证下,禁用:1) 匿名身份验证 2) 表单身份验证

然后将以下内容添加到您的控制器中,以处理测试与服务器部署:

string sUserName = null;
string url = Request.Url.ToString();

if (url.Contains("localhost"))
  sUserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
else
  sUserName = User.Identity.Name;
于 2019-05-01T17:15:14.610 回答
1

如果有人还在读这篇文章,那么访问我以下列方式使用的 cshtml 文件。

<li>Hello @User.Identity.Name</li>
于 2020-10-24T14:56:53.777 回答