public ActionResult Login(LoginModel model, string returnUrl) { if (string.IsNullOrEmpty(model.UserName)) { ModelState.AddModelError("", "账号不能为空!"); return View(model); } if (string.IsNullOrEmpty(model.Password)) { ModelState.AddModelError("", "密码不能为空!"); return View(model); } string errorMessage = string.Empty; var loginSuccessful = Login(model.UserName.Trim(), model.Password.Trim(), out errorMessage); if (loginSuccessful == null) { ModelState.AddModelError("", errorMessage); return View(model); } _formsAuthentication.SetAuthCookie(model.UserName, true); _contextService.SetCookie("role", CurrentUser.RoleId.ToString()); _contextService.NickName = CurrentUser.NickName; _contextService.DepId = CurrentUser.DepId.ToString(); _contextService.UserId = CurrentUser.Id.ToString(); _contextService.UserPhoto = ConfigurationManager.AppSettings["USER_AVATAR"] + CurrentUser.UserInfoPhoto; _contextService.SpaceTreeHtml = _spaceService.GetOrSetSpaceTree(_contextService.UserId); HttpCookie cookie = new HttpCookie("USER_COOKIE"); if (model.RememberMe) { //所有的验证信息检测之后,如果用户选择的记住密码,则将用户名和密码写入Cookie里面保存起来。 cookie.Values.Add("UserName", Md5Util.Encrypt(model.UserName.Trim())); cookie.Values.Add("UserPassword", Md5Util.Encrypt(model.Password.Trim())); //这里是设置Cookie的过期时间,这里设置30天的时间,过了时间之后状态保持自动清空。 cookie.Expires = DateTime.Now.AddDays(30); Response.Cookies.Add(cookie); } else { //如果用户没有选择记住密码,那么立即将Cookie里面的信息情况,并且设置状态保持立即过期。 var httpCookie = Response.Cookies["USER_COOKIE"]; if (httpCookie != null) { httpCookie.Value = null; httpCookie.Expires = DateTime.Now; } } _logger.Info("登录成功:user:" + model.UserName); return loginSuccessful; }
// // POST: /Account/LogOff //[ValidateAntiForgeryToken] public ActionResult LogOut() { if (CurrentUser != null) { _logger.Info("注销退出:user:" + CurrentUser.UserName); //_userLogService.Log(new UserLogContract() { IpAddress = CerCommon.GetIp(), Message = "注销退出", UserId = CurrentUser.Id, FromClient = "主系统" }); } //WebSecurity.Logout(); HttpCookie cookie = new HttpCookie("USER_COOKIE"); //读取保存的Cookie信息 HttpCookie cookies = Request.Cookies["USER_COOKIE"]; var model = new LoginModel(); if (cookies != null && !string.IsNullOrEmpty(cookies.Value)) { //如果Cookie不为空,则将Cookie里面的用户名和密码读取出来赋值给前台的文本框。 model.UserName = Md5Util.Decrypt(cookies["UserName"]); model.Password = Md5Util.Decrypt(cookies["UserPassword"]); if (!string.IsNullOrEmpty(cookies["AutoLogin"])) { model.AutoLogin = bool.Parse(Md5Util.Decrypt(cookies["AutoLogin"])); } //这里依然把记住密码的选项给选中。 model.RememberMe = true; } if (model.RememberMe) { //所有的验证信息检测之后,如果用户选择的记住密码,则将用户名和密码写入Cookie里面保存起来。 cookie.Values.Add("UserName", Md5Util.Encrypt(model.UserName.Trim())); cookie.Values.Add("UserPassword", Md5Util.Encrypt(model.Password.Trim())); cookie.Values.Add("AutoLogin", Md5Util.Encrypt(false.ToString())); //这里是设置Cookie的过期时间,这里设置7天的时间,过了时间之后状态保持自动清空。 cookie.Expires = DateTime.Now.AddDays(7); Response.Cookies.Add(cookie); } ActionResult logOff = Logout(); _formsAuthentication.SignOut(); _contextService.SetCookie("role", ""); _contextService.NickName = null; _contextService.DepId = string.Empty; return logOff; }
// // GET: /Account/Login //[AllowAnonymous] public ActionResult Login(string returnUrl) { _logger.Info("登录页面:访问IP:" + CerCommon.GetIp()); if (CurrentUser != null && !(CurrentUser is EmptyUserContract)) { _formsAuthentication.SetAuthCookie(CurrentUser.UserName, false); _contextService.SetCookie("role", CurrentUser.RoleId.ToString()); _contextService.NickName = CurrentUser.NickName; _contextService.DepId = CurrentUser.DepId.ToString(); _contextService.UserPhoto = ConfigurationManager.AppSettings["USER_AVATAR"] + CurrentUser.UserInfoPhoto; _logger.Info(CurrentUser.Id + "登录成功" + "文档管理系统"); return Redirect("/home/index"); } var model = new LoginModel(); //读取保存的Cookie信息 HttpCookie cookies = Request.Cookies["USER_COOKIE"]; if (cookies != null && !string.IsNullOrEmpty(cookies.Value)) { //如果Cookie不为空,则将Cookie里面的用户名和密码读取出来赋值给前台的文本框。 model.UserName = Md5Util.Decrypt(cookies["UserName"]); model.Password = Md5Util.Decrypt(cookies["UserPassword"]); //这里依然把记住密码的选项给选中。 model.RememberMe = true; ViewBag.ReturnUrl = returnUrl; if (model.AutoLogin) { return Login(model, returnUrl); } return View(model); } //if (!string.IsNullOrEmpty(returnUrl) && returnUrl.EndsWith("/account/logoff")) //{ // returnUrl = returnUrl.Replace("/account/logoff", "/home/index"); //} ViewBag.ReturnUrl = returnUrl; return View(model); }