ActionFilter、IAuthorizationFilter 权限验证重定向跳转到其它页面

方法一:

public class IsAllowAttribute: ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (!Ctx.User.IsAllow) 
        {
            filterContext.Result = new RedirectToRouteResult(
                new RouteValueDictionary 
                { 
                    { "controller", "Home" }, 
                    { "action", "Index" }         //需要跳转到的 controller 对应的 action   
                });
        }
    }
}

方法二:

public class AuthorizeActionFilterAttribute : ActionFilterAttribute
{
  public override void OnActionExecuting(FilterExecutingContext filterContext)
  {
    HttpSessionStateBase session = filterContext.HttpContext.Session;  //获取当前用户的Session
    Controller controller = filterContext.Controller as Controller;

    if (controller != null)
    {
      if (session["Login"] == null)
      {
        filterContext.Cancel = true;
        controller.HttpContext.Response.Redirect("./Login");  //重定向到要跳转的页面
      }
    }

    base.OnActionExecuting(filterContext);
  }
}

 

posted @ 2019-07-02 10:47  leco_lin  阅读(629)  评论(0编辑  收藏  举报