如何在 ASP.NET MVC 中获取当前用户
在表单模型中,我曾经通过以下方式获取当前登录用户:
Page.CurrentUser
How do I get the current user inside a controller class in ASP.NET MVC?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
在Asp.net Mvc Identity 2中,您可以通过以下方式获取当前用户名:
In Asp.net Mvc Identity 2,You can get the current user name by:
如果有人仍在阅读本文,请按照以下方式访问 cshtml 文件。
If any one still reading this then, to access in cshtml file I used in following way.
在 IIS 管理器的“身份验证”下,禁用:
1) 匿名认证
2) 表单身份验证
然后将以下内容添加到您的控制器中,以处理测试与服务器部署:
In the IIS Manager, under Authentication, disable:
1) Anonymous Authentication
2) Forms Authentication
Then add the following to your controller, to handle testing versus server deployment:
如果您需要从控制器内获取用户,请使用控制器的
User
属性。 如果您需要从视图中获取它,我会在ViewData
中填充您特别需要的内容,或者您可以直接调用 User,因为我认为它是ViewPage
的属性。If you need to get the user from within the controller, use the
User
property of Controller. If you need it from the view, I would populate what you specifically need in theViewData
, or you could just call User as I think it's a property ofViewPage
.我发现
User
有效,即User.Identity.Name
或User.IsInRole("Administrator")
。I found that
User
works, that is,User.Identity.Name
orUser.IsInRole("Administrator")
.尝试
HttpContext.Current.User
。Try
HttpContext.Current.User
.您可以在 ASP.NET MVC4 中获取用户名,如下所示:
You can get the name of the user in ASP.NET MVC4 like this:
我意识到这确实很旧,但我刚刚开始使用 ASP.NET MVC,所以我想我应该把我的两分钱放在:
Request.IsAuthenticated
告诉您用户是否经过身份验证。Page.User.Identity
为您提供登录用户的身份。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:
Request.IsAuthenticated
tells you if the user is authenticated.Page.User.Identity
gives you the identity of the logged-in user.我使用:
我不确定这是否适用于 ASP.NET MVC,但值得一试:)
I use:
I am not sure this will work in ASP.NET MVC, but it's worth a shot :)
登录用户名:
System.Web.HttpContext.Current.User.Identity.Name
getting logged in username:
System.Web.HttpContext.Current.User.Identity.Name
UserName with:
但如果你只需要获取ID,你可以使用:
所以,你可以直接获取User ID:
UserName with:
But if you need to get just the ID, you can use:
So, you can get directly the User ID:
为了引用在控制器中使用内置于 ASP.NET MVC 4 的简单身份验证创建的用户 ID 以进行过滤(如果您首先使用数据库和实体框架 5 来生成代码优先绑定,并且您的表的结构如下,这会很有帮助)使用了 userID 的外键),
一旦添加 using 语句就可以使用
In order to reference a user ID created using simple authentication built into ASP.NET MVC 4 in a controller for filtering purposes (which is helpful if you are using database first and Entity Framework 5 to generate code-first bindings and your tables are structured so that a foreign key to the userID is used), you can use
once you add a using statement
我们可以使用以下代码来获取 ASP.Net MVC 中当前登录的用户:
另外
We can use following code to get the current logged in User in ASP.Net MVC:
Also
此页面可能就是您要寻找的内容:
在 MVC3 中使用 Page.User.Identity.Name
你只需要 <代码>用户.身份.名称。
This page could be what you looking for:
Using Page.User.Identity.Name in MVC3
You just need
User.Identity.Name
.使用 System.Security.Principal.WindowsIdentity.GetCurrent().Name。
这将获取当前登录的 Windows 用户。
Use
System.Security.Principal.WindowsIdentity.GetCurrent().Name
.This will get the current logged-in Windows user.
不管怎样,在 ASP.NET MVC 3 中,您可以只使用 User 来返回当前请求的用户。
For what it's worth, in ASP.NET MVC 3 you can just use User which returns the user for the current request.
如果您在登录页面内,例如在 LoginUser_LoggedIn 事件中,Current.User.Identity.Name 将返回空值,因此您必须使用 yourLoginControlName.UserName 属性。
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.
您可以使用以下代码:
You can use following code:
如果您碰巧在 Intranet 上的 Active Directory 中工作,这里有一些提示:
(Windows Server 2012)
在 Web 服务器上运行任何与 AD 通信的内容都需要大量的更改和耐心。 由于在 Web 服务器上运行时与本地 IIS/IIS Express 上运行时,它以 AppPool 的身份运行,因此您必须将其设置为模拟访问该站点的任何人。
当 ASP.NET MVC 应用程序在网络内的 Web 服务器上运行时,如何获取活动目录中的当前登录用户:
您必须将与“活动目录上下文”相关的所有调用包装在下面,以便它起作用作为获取 AD 信息的托管环境:
您还必须在 web.config 中将
impersonate
设置为 true:您必须在 web.config 中启用 Windows 身份验证:
If you happen to be working in Active Directory on an intranet, here are some tips:
(Windows Server 2012)
Running anything that talks to AD on a web server requires a bunch of changes and patience. Since when running on a web server vs. local IIS/IIS Express it runs in the AppPool's identity so, you have to set it up to impersonate whoever hits the site.
How to get the current logged-in user in an active directory when your ASP.NET MVC application is running on a web server inside the network:
You must wrap any calls having to do with 'active directory context' in the following so it's acting as the hosting environment to get the AD information:
You must also have
impersonate
set to true in your web.config:You must have Windows authentication on in web.config: