public ActionResult Login(UserLoginFormModel userModel) { string result = "fail"; //对比验证码 if (Session["validateCode"].Equals(userModel.ValidateCode)) { UserLogin loginModel = new UserLogin() { UName = userModel.UName, UPwd = Md5Helper.GetMd5(userModel.UPwd) }; bool loginOk = userInfoBll.Login(loginModel); if (loginOk) { result = "yes"; //Session["UserLogin"] = loginModel; //使用mm+cookie进行登录用户的存储 string key = Guid.NewGuid().ToString(); MMhelper.Set(key, loginModel, DateTime.Now.AddMinutes(20)); HttpCookie cookie = new HttpCookie("userId"); cookie.Value = key; cookie.Expires = DateTime.Now.AddMinutes(20); Response.Cookies.Add(cookie); } else { result = "no"; } } else { result = "validateFail"; } return(Content(result)); }
protected override void OnAuthorization(AuthorizationContext filterContext) { base.OnAuthorization(filterContext); //if (filterContext.HttpContext.Session["UserLogin"] == null) //{ // filterContext.Result = new RedirectResult(Url.Action("Index", "UserLogin")); //} if (filterContext.HttpContext.Request.Cookies["userId"] == null) { filterContext.Result = new RedirectResult(Url.Action("Index", "UserLogin")); } else { string key = filterContext.HttpContext.Request.Cookies["userId"].Value.ToString(); var userlogin = MMhelper.Get(key) as UserLogin; if (userlogin == null) { filterContext.Result = new RedirectResult(Url.Action("Index", "UserLogin")); return; } filterContext.HttpContext.Response.Cookies["userId"].Expires = DateTime.Now.AddDays(20); MMhelper.Set(key, userlogin, DateTime.Now.AddMinutes(20)); } }