Session登录验证

最近看到一篇帖子是自己写一个简单的mvc框架挺有意思的http://www.cnblogs.com/edisonchou/p/5211645.html
突然想到自己之前在做用户登录验证方面还可以再改进一下

插一句话,先看mvc页面执行顺序ASP.NET MVC ActionFilterAttribute的执行顺序https://www.cnblogs.com/zkwarrior/p/4794868.html

先说应该怎么做吧
新建一个BaseController继承Controller,然后其他业务控制器继承BaseController(LoginController直接继承Controller,不然会循环),其实这个BaseController作用很大的,不光可以做登录验证,我之前有用它做过权限控制。
在BaseController中重写OnActionExecuted方法,参考自https://blog.csdn.net/zy0421911/article/details/51284145

我的代码:

public class BaseController : Controller
    {
        protected override void OnActionExecuting(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
            if (Session["userid"] == null)
            {
                filterContext.Result = Redirect("/Login/Index");
            }
        }
	}

另外在LoginController的登录页写个重定向的参数,可以在登录之后直接重定向到原本用户想要去的页面,另外此方法不能验证ajax提交方式

public class LoginController : Controller
    {
        //
        // GET: /Login/
        public ActionResult Index(string oldController,string oldIndex)
        {
            ViewBag.controller = oldController;
            ViewBag.index = oldIndex;
            return View();
        }
	}

代码下载地址:https://pan.baidu.com/s/19ddvnTJlA8I_BvtB8OHFHg
本来我的想法是在Global.asax处写一个判断是否登录的,结果不行,Global是整个应用程序的入口,而Session是用户会话,另外附上我之前的做法吧

在Default控制器中写了个Action拦截器

public class LoginFilterAttribute : ActionFilterAttribute
    {
        private JXGLEntities db = new JXGLEntities();
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string ActionName = filterContext.ActionDescriptor.ActionName;
            var session = System.Web.HttpContext.Current.Session["user"];
            if (session == null)
            {

                if (ActionName == "Index")
                {
                    filterContext.Result = new RedirectToRouteResult("Default", new System.Web.Routing.RouteValueDictionary(new { action = "Login" }));
                }
                else
                {
                    filterContext.Result = new RedirectToRouteResult("Default", new System.Web.Routing.RouteValueDictionary(new { action = "Login", warning = "1" })
                    );
                }
            }
            else
            {
                var page = (from c in db.T_PAGE where c.ADDRESS == ActionName select c).FirstOrDefault();
                if (page != null)
                {
                    var user = session as T_USER;
                    string role = user.POWER.ToString();
                    string msg = PageData.IsChecked(page.ID, role);
                    if (msg != "checked =\"checked\"")
                    {
                        filterContext.Result = new RedirectToRouteResult("Default", new System.Web.Routing.RouteValueDictionary(new { action = "PageReturn" }));
                    }
                }
            }
        }
    }

这样做麻烦的就是每个页面都要在ActionResult上加[LoginFilterAttribute],不过相比于重写OnActionExecuted方法还是有好处的,更灵活嘛。还可以验证ajax提交方式(没试过,应该可以,有兴趣可以试试)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值