在表单模型中,我曾经通过以下方式获取当前登录用户:
Page.CurrentUser
如何在 ASP.NET MVC 的控制器类中获取当前用户?
在表单模型中,我曾经通过以下方式获取当前登录用户:
Page.CurrentUser
如何在 ASP.NET MVC 的控制器类中获取当前用户?
如果您需要从控制器中获取用户,请使用User
Controller 的属性。如果您从视图中需要它,我会在 中填充您特别需要的内容ViewData
,或者您可以只调用 User ,因为我认为它是ViewPage
.
我发现这User
行得通,也就是说,User.Identity.Name
或者User.IsInRole("Administrator")
。
试试HttpContext.Current.User
。
公共共享属性 Current() 作为 System.Web.HttpContext
的 System.Web.HttpContext 成员摘要:
获取或设置当前 HTTP 请求的 System.Web.HttpContext 对象。返回值:
当前 HTTP 请求的 System.Web.HttpContext
您可以像这样在 ASP.NET MVC4 中获取用户的名称:
System.Web.HttpContext.Current.User.Identity.Name
我意识到这真的很老了,但我刚刚开始使用 ASP.NET MVC,所以我想我会坚持我的两分钱:
Request.IsAuthenticated
告诉您用户是否已通过身份验证。Page.User.Identity
为您提供登录用户的身份。我用:
Membership.GetUser().UserName
我不确定这是否适用于 ASP.NET MVC,但值得一试:)
登录用户名:System.Web.HttpContext.Current.User.Identity.Name
用户名:
User.Identity.Name
但是,如果您只需要获取 ID,则可以使用:
using Microsoft.AspNet.Identity;
因此,您可以直接获取用户 ID:
User.Identity.GetUserId();
我们可以使用以下代码在 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'
为了引用使用控制器中内置于 ASP.NET MVC 4 中的简单身份验证创建的用户 ID 以进行过滤(如果您首先使用数据库和 Entity Framework 5 生成代码优先绑定并且您的表的结构如此,这将很有帮助使用了用户 ID 的外键),您可以使用
WebSecurity.CurrentUserId
一旦添加了 using 语句
using System.Web.Security;
此页面可能是您要查找的内容:
在 MVC3 中使用 Page.User.Identity.Name
你只需要User.Identity.Name
.
使用System.Security.Principal.WindowsIdentity.GetCurrent().Name
.
这将获取当前登录的 Windows 用户。
对于它的价值,在 ASP.NET MVC 3 中,您可以只使用返回当前请求的用户的用户。
如果您在登录页面内,例如在 LoginUser_LoggedIn 事件中,Current.User.Identity.Name 将返回一个空值,因此您必须使用 yourLoginControlName.UserName 属性。
MembershipUser u = Membership.GetUser(LoginUser.UserName);
您可以使用以下代码:
Request.LogonUserIdentity.Name;
IPrincipal currentUser = HttpContext.Current.User;
bool writeEnable = currentUser.IsInRole("Administrator") ||
...
currentUser.IsInRole("Operator");
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));
如果您恰好在 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" />
在 Asp.net Mvc Identity 2 中,可以通过以下方式获取当前用户名:
var username = System.Web.HttpContext.Current.User.Identity.Name;
在 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;
如果有人还在读这篇文章,那么访问我以下列方式使用的 cshtml 文件。
<li>Hello @User.Identity.Name</li>