public ActionResult Login() { if (this.ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains("loginusername")) { string lastUserName = this.ControllerContext.HttpContext.Request.Cookies["loginusername"].Value; LoginModel m = new LoginModel(); m.UserName = lastUserName; m.RememberMe = true; return View(m); } else { return View(); } }
public ActionResult Login(LoginModel model, string returnUrl, FormCollection values) { if (ModelState.IsValid) { SqlServerDAL.DA_Custom daCustom = new SqlServerDAL.DA_Custom(); string err = ""; Message.CustomInfo custom = daCustom.LoginByPhone(model.UserName, model.Password, ref err); if (custom != null) { if (model.RememberMe) { HttpCookie cookie = new HttpCookie("loginusername"); cookie.Value = model.UserName; this.ControllerContext.HttpContext.Response.Cookies.Add(cookie); } Session.Add("loginedcustom", custom); //记录最近登录时间 daCustom.UpdateCustomLastLoginTime(custom.CustomID); FormsAuthentication.SetAuthCookie(custom.CustomName, false); if (Url.IsLocalUrl(returnUrl)) { return Redirect(returnUrl); } else { return RedirectToAction("MyProducts"); //进入个人首页 } } else { if (err != "") { //err不为空,说明用户名密码正确,但未认证 ModelState.AddModelError("", err); } else { ModelState.AddModelError("", "用户名密码错误,登录失败!"); } } } // 如果我们进行到这一步时某个地方出错,则重新显示表单 return View(model); }