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

C# 如何在ASP.NET MVC中获取当前用户,c#,.net,asp.net-mvc,iis,forms-authentication,C#,.net,Asp.net Mvc,Iis,Forms Authentication,在表单模型中,我通常通过以下方式获取当前登录用户: Page.CurrentUser 如何在ASP.NET MVC中的控制器类中获取当前用户?我使用: Membership.GetUser().UserName 我不确定这在ASP.NET MVC中是否有效,但值得一试:)尝试HttpContext.Current.User 公共共享属性当前()为 System.Web.HttpContext System.Web.HttpContext的成员 总结: 获取或设置当前HTTP请求的System

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

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

我使用:

Membership.GetUser().UserName

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

尝试
HttpContext.Current.User

公共共享属性当前()为 System.Web.HttpContext
System.Web.HttpContext的成员

总结:
获取或设置当前HTTP请求的System.Web.HttpContext对象

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


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

的一个属性。我意识到这很旧,但我刚刚开始使用ASP.NET MVC,所以我想我应该投入两分钱:

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

我发现
用户
有效,也就是说,
用户.Identity.Name
用户.IsInRole(“管理员”)
登录用户名:
系统.Web.HttpContext.Current.User.Identity.Name
,在ASP.NET MVC 3中,您可以只使用User返回当前请求的用户。

如果您在登录页面中,例如在LoginUser\u LoggedIn事件中,current.User.Identity.Name将返回空值,因此您必须使用LoginControl.UserName属性

IPrincipal currentUser = HttpContext.Current.User;
bool writeEnable = currentUser.IsInRole("Administrator") ||
        ...
                   currentUser.IsInRole("Operator");
MembershipUser u = Membership.GetUser(LoginUser.UserName);

此页面可能就是您要查找的内容:


您只需要
User.Identity.Name

就可以引用在控制器中使用ASP.NET MVC 4内置的简单身份验证创建的用户ID,以便进行过滤(如果您使用database first和Entity Framework 5生成代码优先绑定,并且您的表是结构化的,以便使用用户ID的外键,这将非常有用),您可以使用

WebSecurity.CurrentUserId
添加using语句后

using System.Web.Security;

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

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

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


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

如果您碰巧在intranet上的Active Directory中工作,以下是一些提示:

(Windows Server 2012)

在web服务器上运行与AD对话的任何内容都需要大量的更改和耐心。因为在web服务器上运行与在本地IIS/IIS Express上运行相比,它是以应用程序池的身份运行的,因此,您必须将其设置为模拟访问该站点的任何人

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

// 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...
您必须将与“active directory上下文”有关的任何调用包装在以下内容中,以便它充当宿主环境来获取广告信息:

using (HostingEnvironment.Impersonate()){ ... }
您还必须在web.config中将
impersonate
设置为true:

<system.web>
    <identity impersonate="true" />
<authentication mode="Windows" />

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

<system.web>
    <identity impersonate="true" />
<authentication mode="Windows" />

用户名,包括:

User.Identity.Name
但如果您只需要获取ID,您可以使用:

using Microsoft.AspNet.Identity;
因此,您可以直接获取用户ID:

User.Identity.GetUserId();

您可以使用以下代码:

Request.LogonUserIdentity.Name;

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

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

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

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


在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>
  • 你好@User.Identity.Name

  • 此会员资格类别来自何处?默认情况下,IntelliSense无法识别它。System.Web.Security命名空间。我不确定这在MVC中是否有用,但我将其用于Web表单的登录控件。它在任何使用成员资格提供程序的ASP.NET应用程序中都很有用。这实际上会发出数据库请求。HttpContext.Current.User没有。显然HttpContext没有名为“Current”的属性。我相信你们在谈论两件不同的事情。System.Web.HttpContext和静态属性:System.Web.Mvc.Controller.HttpContext(它没有“当前”属性。它在非Mvc环境下工作,这正是我所需要的。谢谢!:)“或者你可以调用用户,因为我认为它是ViewPage的属性”-那么你的意思是在视图中使用Page.User吗?是的,你可以像,“嗨,@User.Identity.Name!“在cshtml中。不幸的是,这在MVC 5中似乎不再有效。不确定为什么=/Only
    System.Security.Principal.WindowsIdentity.GetCurrent().Name
    MVC 5
    中工作。但是,这会使用用户名提取域名。例如,domain\username只是为了添加到其中,如果您在表单之外的类中工作,则您需要导入System.Web并进一步使用
    HttpContext.Current.User.Identity.Name
    ,或者直接使用完整语法:
    System.Web.HttpContext.Current.User.Identity.Name
    在控制器内工作,如果使用ViewModel,则可以从控制器继承或遵循Paul的建议。虽然此代码可以回答问题,但最好包含一些上下文,解释其工作原理,并描述何时使用。仅代码的答案是not在长期运行中很有用。System.Security.Principal.WindowsIdentity.GetCurrent().Name返回当前登录到Windows的用户,OP请求当前登录到网站的用户。如果收到此错误,则“System.web.HttpContextBase”不包含