Asp.net 如何在MVC中找到用户名和角色?

Asp.net 如何在MVC中找到用户名和角色?,asp.net,asp.net-mvc-3,Asp.net,Asp.net Mvc 3,是否有某种方法可以在MVC中找到用户和用户角色?.Net中的用户也可以同时拥有多个角色吗?标准ASP.Net方式: public ActionResult Test() { if (User.IsInRole("role name")) { //do something } return View(); } 是的,用户可以担任多个角色。标准ASP.NET方式: public ActionResult Test() { if

是否有某种方法可以在MVC中找到用户和用户角色?.Net中的用户也可以同时拥有多个角色吗?

标准ASP.Net方式:

  public ActionResult Test()
  {
    if (User.IsInRole("role name"))
    {
      //do something
    }
    return View();
  }
是的,用户可以担任多个角色。

标准ASP.NET方式:

  public ActionResult Test()
  {
    if (User.IsInRole("role name"))
    {
      //do something
    }
    return View();
  }

是的,一个用户可以扮演多个角色。

您将在
HttpContext
对象中找到用户及其角色(身份和原则)对象,如下所示

HttpContext.User
HttpContext.User.Identity
您必须重写basecontroller上的onauthorize方法

public class HomeController : Controller
    {
        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            //filterContext.HttpContext.User.Identity  
            base.OnAuthorization(filterContext);
        }
}

您将在
HttpContext
对象中找到用户及其角色(身份和原则)对象,如下所示

HttpContext.User
HttpContext.User.Identity
您必须重写basecontroller上的onauthorize方法

public class HomeController : Controller
    {
        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            //filterContext.HttpContext.User.Identity  
            base.OnAuthorization(filterContext);
        }
}
使用

希望能有帮助。祝你好运

使用


希望能有帮助。祝你好运

对不起。也许我没有解释清楚。我想从我的MVC代码中找出当前用户和该用户的角色。谢谢。当您在控制器中时,您有一个名为User的属性(我在代码块中使用的属性)对不起。也许我没有解释清楚。我想从我的MVC代码中找出当前用户和该用户的角色。谢谢。当您在控制器中时,您有一个名为User的属性(我在代码块中使用的属性)不是开玩笑,而是取决于您如何存储用户和角色您是否使用asp.net成员资格???是的,我使用asp.net成员资格。不是开玩笑,而是取决于您如何存储用户和角色您是否使用asp.net成员资格???是的,我使用asp.net成员资格。您需要添加
使用系统安全在顶部。您需要使用System.Web.Security添加
在顶部。