/* * ************控制器私有方法************ */ private bool ValidateUserCode(string code) { //先获取服务器端的code if (string.IsNullOrEmpty(code)) { return(false); } string servercode = String.Empty; try { servercode = CacheResolver.GetCache(ValidateCodeId.GetValidateCodeId()) as string; } catch (NullReferenceException) { //缓存的数据失效了 return(false); } if (null == servercode) { return(false); } if (code != servercode) { return(false); } return(true); }
protected void Application_BeginRequest(object sender, EventArgs e) { string key = Request["sessionid"]; SessionModel model = null; bool state = false; try { model = CacheResolver.GetCache(key) as SessionModel; if (null != model) { state = true; } } catch (ArgumentNullException) { //没有缓存键,第一次登陆这个网站 key = Guid.NewGuid().ToString("N"); model = new SessionModel(); } catch (NullReferenceException) { model = new SessionModel(); } if (!state) { //开始重新写入 CacheResolver.SetCache(key, model); //开始吧key写入到cookie中去 HttpContext.Current.Response.Cookies.Add(new HttpCookie("sessionid", key)); } }
public SessionModel GetCurrentUser(string sessionid) { if (string.IsNullOrEmpty(sessionid)) { throw new ArgumentNullException("用户SessionId为空", innerException: null); } SessionModel sessionModel = null; try { sessionModel = CacheResolver.GetCache(sessionid) as SessionModel; } catch (NullReferenceException) { throw new UserException("用户信息已经失效,请重新登陆"); } return(sessionModel); }
public override void OnActionExecuting(ActionExecutingContext filterContext) { string sessionid = HttpContext.Current.Request["sessionid"]; SessionModel sessionModel = null; bool state = false; try { sessionModel = CacheResolver.GetCache(sessionid) as SessionModel; } catch (ArgumentNullException) { //第一次登陆 state = true; } catch (NullReferenceException) { //缓存数据失效 state = true; } if (null == sessionModel.User) { state = true; } if (state) { //跳转登录页面 filterContext.HttpContext.Response.Redirect("/Home/Index", true); } BaseController ba = filterContext.Controller as BaseController; if (null == ba) { throw new UserException("使用UserSimp属性必须要求控制器继承自BaseController"); } ba.sessionModel = sessionModel; base.OnActionExecuting(filterContext); }