ActionFilter、IAuthorizationFilter 權限驗證重定向跳轉到其它頁面

方法一:session

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   
                });
        }
    }
}

方法二:ide

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);
  }
}
相關文章
相關標籤/搜索