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); } }
你好,这是 Leco's 开发笔记,转载请注明出处:http://www.cnblogs.com/leco