public ActionResult Login(LoginModel model, string returnUrl) { if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe)) { return RedirectToLocal(returnUrl); } // 如果我们进行到这一步时某个地方出错,则重新显示表单 ModelState.AddModelError("", "提供的用户名或密码不正确。"); return View(model); }
public ActionResult LoginIn(LoginModel model) { AdminUserBLL bll = new AdminUserBLL(); string md5_pwd = Des.GetMD5String(model.Password); AdminUser user = bll.Login(model.UserName, md5_pwd); if(user == null || user.UserName != model.UserName || user.Password != md5_pwd) { ViewBag.LoginInfo = model; ViewBag.Message = "登录失败,用户名或密码错误!"; return View("Login"); } //为提供的用户名提供一个身份验证的票据 FormsAuthentication.SetAuthCookie(user.UserName, true, FormsAuthentication.FormsCookiePath); //把用户对象保存在票据里 FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, user.UserName, DateTime.Now, DateTime.Now.AddTicks(FormsAuthentication.Timeout.Ticks), false, JsonConvert.SerializeObject(user)); //加密票据 string hashTicket = FormsAuthentication.Encrypt(Ticket); HttpCookie userCookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket); Response.Cookies.Add(userCookie); return RedirectToAction("UserInfo","AdminUser"); }