为了实现保持登录状态,可以用cookie来解决这一问题
假设过期时间为30分钟,校验发生在服务器,借助过滤器,可以这样写
| 代码如下 | 复制代码 |
|
publicclassPowerFilter : AuthorizeAttribute { publicoverridevoidOnAuthorization(AuthorizationContext filterContext) { var cookie = HttpContext.Current.Request.Cookies["loginInfo"]; if(null== cookie) { filterContext.Result =newRedirectResult("/admin/login/index"); } else { cookie.Expires = DateTime.Now.AddMinutes(30); HttpContext.Current.Response.Cookies.Remove("loginInfo"); HttpContext.Current.Response.Cookies.Add(cookie); } } } | |
但是页面直接跳转了,也没有一个提示,显得不是很友好,可以这样
| 代码如下 | 复制代码 |
|
publicclassPowerFilter : AuthorizeAttribute { publicoverridevoidOnAuthorization(AuthorizationContext filterContext) { var cookie = HttpContext.Current.Request.Cookies["loginInfo"]; if(null== cookie) { filterContext.Result =newContentResult() { Content =string .Format(" ","/admin/login/index") }; } else { cookie.Expires = DateTime.Now.AddMinutes(30); HttpContext.Current.Response.Cookies.Remove("loginInfo"); HttpContext.Current.Response.Cookies.Add(cookie); } } } } | |
但是,假如是ajax请求呢?
| 代码如下 | 复制代码 |
|
publicclassPowerFilter : AuthorizeAttribute { publicoverridevoidOnAuthorization(AuthorizationContext filterContext) { var cookie = HttpContext.Current.Request.Cookies["loginInfo"]; if(null== cookie) { if(!filterContext.HttpContext.Request.IsAjaxRequest()) { filterContext.Result =newContentResult() { Content =string .Format(" ","/admin/login/index") }; } else { filterContext.Result =newJsonResult() { Data =new{ logoff =true,logurl ="/admin/login/index"}, ContentType =null, ContentEncoding =null, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } } else { cookie.Expires = DateTime.Now.AddMinutes(30); HttpContext.Current.Response.Cookies.Remove("loginInfo"); HttpContext.Current.Response.Cookies.Add(cookie); } } } | |
模拟极限越野
模拟极限越野开着大巴在各种奇葩路况里送乘客。山路弯道多得让人
模拟火车
模拟火车让你当回真正的火车司机,得先学会看信号灯、操作控制杆
可口的披萨美味的披萨
可口的披萨美味的披萨带您体验经营一家披萨店的乐趣。游戏画风温
定制虚拟偶像最新版
定制虚拟偶像最新版是一款超级经典有趣的日本模拟装扮类型的手游
病娇模拟器手机版正版(yandere simulator)
病娇模拟器最新版手游是一款萌妹二次元模拟游戏,玩家在游戏中可