2014-09-10 2 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>())); 
    } 
} 

Если у пользователя нет ролика 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 Роли =" 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

или попытаться спросить дядя Google для "кастом 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(); 
} 

и View():

@if(ViewBag.Error != null) 
{ 
    <p>@ViewBag.Error</p> 
} 
Смежные вопросы