登陆验证(过滤器)

来源:互联网 发布:网络棋牌充值漏洞 编辑:程序博客网 时间:2024/05/01 13:00

我的mvc网站使用session存储用户信息,之前一直是每个页面判断session是否为空,感觉好麻烦,后来在网上看了看过滤器的一些知识,做了一些改进(更适合我)。
首先是找到的过滤器用法:
http://www.studyofnet.com/news/257.html

网友实例
通过配置配置authentication 来验证控制 Login 登录
http://blog.csdn.net/jintougao/article/details/42267807
实例
http://blog.csdn.net/jintougao/article/details/9281275
我的改进
重写OnActionExecuting方法

public class LoginFilterAttribute:ActionFilterAttribute    {        public override void OnActionExecuting(ActionExecutingContext filterContext)        {            if(System.Web.HttpContext.Current.Session["user"] == null)            {                filterContext.Result = new RedirectToRouteResult("Default",                    new System.Web.Routing.RouteValueDictionary(new { action = "Login" })                    );            }        }    }

然后在需要验证的页面头部加上[LoginFilterAttribute]

/// <summary>/// 主页/// </summary>/// <returns></returns>[LoginFilterAttribute]public ActionResult Index()
0 0
原创粉丝点击