ASP.Net MVC 3 Redirect UnAuthorized User not to loginUrl

前端 未结 6 1584
庸人自扰
庸人自扰 2020-12-23 22:40

i have a project using ASP.Net MVC3 and using membership for roles. i use authorize in every controller. eg:

[Authorize(Roles = \"Administrator\")]
    publ         


        
6条回答
  •  自闭症患者
    2020-12-23 23:13

    i solved my problem. i only do this :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    public class MyAuthorize : AuthorizeAttribute
    {
       protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
       {
         //you can change to any controller or html page.
         filterContext.Result = new RedirectResult("/cpanel/roles/unauthorize");
    
       }
     }
    

    and apply MyAuthorize to class or action:

    [MyAuthorize]
    public class AdminController :Controller
    {
    }
    

    thats it.

提交回复
热议问题