如何在ASP.NET MVC中获取当前用户

问题描述 投票:257回答:20

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

Page.CurrentUser

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

c# .net asp.net-mvc iis forms-authentication
20个回答
252
投票

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


5
投票

这个页面可能是你要找的: Using Page.User.Identity.Name in MVC3

你只需要User.Identity.Name


5
投票

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

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


5
投票

我们可以使用以下代码来获取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'

4
投票

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


4
投票

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

MembershipUser u = Membership.GetUser(LoginUser.UserName);

3
投票
IPrincipal currentUser = HttpContext.Current.User;
bool writeEnable = currentUser.IsInRole("Administrator") ||
        ...
                   currentUser.IsInRole("Operator");

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));

2
投票

如果您恰好在Intranet上的Active Directory中工作,请参阅以下提示:

(Windows Server 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()){ ... }

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

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

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

<authentication mode="Windows" />

2
投票

您可以使用以下代码:

Request.LogonUserIdentity.Name;

1
投票

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

var username = System.Web.HttpContext.Current.User.Identity.Name;

188
投票

我发现User的作品,即User.Identity.NameUser.IsInRole("Administrator")


0
投票

在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;

56
投票

试试HttpContext.Current.User

Public Shared Property Current()As System.Web.HttpContext System.Web.HttpContext的成员

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

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


36
投票

您可以在ASP.NET MVC4中获取用户的名称,如下所示:

System.Web.HttpContext.Current.User.Identity.Name

20
投票

我意识到这已经很老了,但我刚刚开始使用ASP.NET MVC,所以我想我会坚持下去:

  • Request.IsAuthenticated告诉您用户是否经过身份验证。
  • Page.User.Identity为您提供登录用户的身份。

16
投票

我用:

Membership.GetUser().UserName

我不确定这会在ASP.NET MVC中有效,但是值得一试:)


11
投票

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


9
投票

为了在过滤目的中引用在ASP.NET MVC 4中内置的简单身份验证创建的用户ID(如果您首先使用数据库而实体框架5生成代码优先绑定并且您的表的结构是如此你可以使用那个用户ID的外键)

WebSecurity.CurrentUserId

一旦添加了using语句

using System.Web.Security;

7
投票

用户名:

User.Identity.Name

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

using Microsoft.AspNet.Identity;

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

User.Identity.GetUserId();
© www.soinside.com 2019 - 2024. All rights reserved.