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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/263486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:24:39  来源:igfitidea点击:

How to get the current user in ASP.NET MVC

c#.netasp.net-mvciisforms-authentication

提问by Serhat Ozgel

In a forms model, I used to get the current logged-in user by:

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

Page.CurrentUser

How do I get the current user inside a controller class in ASP.NET MVC?

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

采纳答案by Haacked

If you need to get the user from within the controller, use the Userproperty of Controller. If you need it from the view, I would populate what you specifically need in the ViewData, or you could just call User as I think it's a property of ViewPage.

如果您需要从控制器内部获取用户,请使用UserController的属性。如果您认为需要的话,我会填充你特别需要的ViewData,或者你可以只调用用户,因为我认为这是一个性质ViewPage

回答by Sean

I use:

我用:

Membership.GetUser().UserName

I am not sure this will work in ASP.NET MVC, but it's worth a shot :)

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

回答by dove

Try HttpContext.Current.User.

试试HttpContext.Current.User

Public Shared Property Current() As System.Web.HttpContext
Member of System.Web.HttpContext

Summary:
Gets or sets the System.Web.HttpContext object for the current HTTP request.

Return Values:
The System.Web.HttpContext for the current HTTP request

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

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

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

回答by jrb

I realize this is really old, but I'm just getting started with ASP.NET MVC, so I thought I'd stick my two cents in:

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

  • Request.IsAuthenticatedtells you if the user is authenticated.
  • Page.User.Identitygives you the identity of the logged-in user.
  • Request.IsAuthenticated告诉您用户是否已通过身份验证。
  • Page.User.Identity为您提供登录用户的身份。

回答by jrb

I found that Userworks, that is, User.Identity.Nameor User.IsInRole("Administrator").

我发现User有效,即,User.Identity.NameUser.IsInRole("Administrator")

回答by tifoz

getting logged in username: System.Web.HttpContext.Current.User.Identity.Name

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

回答by Gediminas Bukauskas

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

回答by Pieter

For what it's worth, in ASP.NET MVC 3 you can just use User which returns the user for the current request.

值得一提的是,在 ASP.NET MVC 3 中,您只需使用 User 即可返回当前请求的用户。

回答by live-love

If you are inside your login page, in LoginUser_LoggedIn event for instance, Current.User.Identity.Name will return an empty value, so you have to use yourLoginControlName.UserName property.

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

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

回答by heriawan

This page could be what you looking for:
Using Page.User.Identity.Name in MVC3

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

You just need User.Identity.Name.

你只需要User.Identity.Name.