MVC action过滤器验证登录

方法一 :  1.创建一个全局action过滤器  (在appstart  的filterconfig中注册   filters.Add(new LoginAttribute());)

       2.不需要登录的contoller或者action  

       则在该类或者action上添加该过滤器特性 (isNeed=false)

方法二: 1.创建一个filter 不在全局注册

     2. 创建 一个baseControler ,然后再basecontroller上边添加该filter特性

               3.需要登录的则继承该basecontroller,不需要登录的则不继承该basecontroller

注意: 1.OnActionExecuting 中   base.OnActionExecuting(filterContext);

              如果当前项目有多个filter则加上 base.OnActionExecuting(filterContext); 

             不添加则不会执行其他的filter

            2.filterContext.Result = new RedirectResult("/User/login");

              在filter里边页面跳转用  filterContext.Result = new RedirectResult("/User/login");

             如果用filterContext.HttpContext.Response.Redirect("/User/login");  则在跳转后还会继续执行  后边的action

          eg: home/index    跳转user/login  后,还会接着执行index/action  里边的方法

原文地址:https://www.cnblogs.com/kaikaichao/p/7097646.html