2014-09-10 24 views
0

我有一个控制器,它要求一个角色名“管理员”:信息,如果requiment角色

它的控制器的一部分:

[Authorize(Roles="Admin")] 
public class RolesAdminController : Controller 
{ 
    public RolesAdminController() 
    { 
    } 

    public RolesAdminController(ApplicationUserManager userManager, 
     ApplicationRoleManager roleManager) 
    { 
     UserManager = userManager; 
     RoleManager = roleManager; 
    } 

    private ApplicationUserManager _userManager; 
    public ApplicationUserManager UserManager 
    { 
     get 
     { 
      return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
     } 
     set 
     { 
      _userManager = value; 
     } 
    } 

    private ApplicationRoleManager _roleManager; 
    public ApplicationRoleManager RoleManager 
    { 
     get 
     { 
      return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>(); 
     } 
     private set 
     { 
      _roleManager = value; 
     } 
    } 

和定义的ApplicationRoleManager其继承RoleManager

public class ApplicationRoleManager : RoleManager<IdentityRole> 
{ 
    public ApplicationRoleManager(IRoleStore<IdentityRole,string> roleStore) 
     : base(roleStore) 
    { 
    } 

    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context) 
    { 
     return new ApplicationRoleManager(new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>())); 
    } 
} 

如果用户没有rolename Admin然后(我不知道如何)被移动到AccountController和方法: public ActionResult Login(string returnUrl) 它的定义:

[HttpGet] 
    [AllowAnonymous] 
    public ActionResult Login(string returnUrl) 
    { 
     ViewBag.ReturnUrl = returnUrl; 
     return View(); 
    } 

现在我想推动信息,以这种方法,如果用户不是管理员,并给信息“嘿!你不必存取权限页面的这一部分,请登录管理员帐户“然后我扩展了这个方法,这种形式:

public ActionResult Login(string returnUrl) 
    { 

     if (returnUrl != null && 
      returnUrl.Contains("Admin") && 
      Request.IsAuthenticated && 
      !User.IsInRole("Admin")) 
     { 
      if (Request.IsAuthenticated) 
       ViewBag.Info = "Hey! You don't have acces to this part of page, please Login to Admin account"; 
      else 
       TempData["Info"] = "Hey! You don't have acces to this part of page, please Login to Admin account"; 
      return RedirectToAction("Index", "Home"); 
     } 
     ViewBag.ReturnUrl = returnUrl; 
     return View(); 
    } 

在我的方式,我知道,所有的控制器,它具有名” admin “,fe RolesAdminController,UserAdminController要求Roles =”Admin“,但它不是最酷的方式:/

它工作正常,但是如果用户(或访客)没有访问权限到控制器?

回答

1

我搜索并找到答案:简单地创建自定义授权类。它很好地解释了这个视频: https://www.youtube.com/watch?feature=player_embedded&v=BsxUsyMSGeA 或在这里: https://www.youtube.com/watch?v=vPyjmuT-lG4&list=PL5MZid1lr-vLd5ec3m1__Xn_1zvyjhOcD&index=26

或尝试问叔叔谷歌的 “创建自定义authorizeAttribute”

例如:

public class RoleAttribute : AuthorizeAttribute 
    { 
     public string UserRole { get; set; } 
     protected override bool AuthorizeCore(HttpContextBase httpContext) 
     { 
      return (httpContext.Request.IsAuthenticated && httpContext.User.IsInRole(UserRole)); 
     } 

     //if AuthorizeCore return false 
     protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) 
     { 
      //add to session 
      filterContext.RequestContext.HttpContext.Session["aa"] = "Hey! You haven't access!"; 
      string action = ""; 
      string controller = ""; 
      //get current action 
      if (filterContext.Controller.ControllerContext.RouteData.Values["action"] != null) 
      { 
       action = filterContext.Controller.ControllerContext.RouteData.Values["action"].ToString(); 
      } 
      //get current controller 
      if (filterContext.Controller.ControllerContext.RouteData.Values["controller"] != null) 
      { 
       controller = filterContext.Controller.ControllerContext.RouteData.Values["controller"].ToString(); 
      } 
      //add some values to temp data 
      filterContext.Controller.TempData.Add("RoleErrors", "Hey! You don't have access!"); 

      //redirect to register method and for example add some info - returnUrl 
      filterContext.Result = new RedirectToRouteResult(
       new System.Web.Routing.RouteValueDictionary(
        new 
        { 
         controller = "Account", 
         action = "Register", 
         returnUrl = string.Format("{0}/{1}",controller,action) 
        }) 
       ); 
     } 
    } 

然后在某些控制器,你可以使用它作为:

[Role(UserRole = "YourRole")] 
public class MyController : Controller 
{ 
    ... 
} 

并且在AccountController中你会被推入方法:

public ActionResult Register(string returnUrl) 
{ 
    ... 
    ViewBag.Error = TempData["RoleErrors"] as string; 
    return View(); 
} 

和视图():

@if(ViewBag.Error != null) 
{ 
    <p>@ViewBag.Error</p> 
} 
相关问题