public ActionResult Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = _db.Users.SingleOrDefault(u => u.LoginName == model.LoginName); if (user != null) { ModelState.AddModelError("", "该登陆名已经存在"); } else { string password = SecurityPsw.SHA1PAssword(model.Password); var newUser = new User { LoginName = model.LoginName, Password = password, RoleId = 2 }; var result = _db.Users.Add(newUser); _db.SaveChanges(); { var _identity = CreateIdentity(newUser); AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(_identity); return(RedirectToAction("Index", "Home")); } } } // 如果我们进行到这一步时某个地方出错,则重新显示表单 return(View(model)); }
public ActionResult Login(LoginViewModel model, string returnUrl) { if (!ModelState.IsValid) { return(View(model)); } var _user = _db.Users.SingleOrDefault(u => u.Iphone == model.AccountNumber || u.LoginName == model.AccountNumber); if (_user == null) { ModelState.AddModelError("AccountNumber", "账号不存在"); } else { string password = SecurityPsw.SHA1PAssword(model.Password); if (_user.Password == password) { var _identity = CreateIdentity(_user); AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = model.RememberMe }, _identity); return(RedirectToLocal(returnUrl)); } else { ModelState.AddModelError("Password", "密码错误"); } } return(View(model)); }
public ActionResult ChangePassword(ChangePasswordViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var user = db.Users.SingleOrDefault(u => u.LoginName == User.Identity.Name); string oldPassword = SecurityPsw.SHA1PAssword(model.OldPassword); if (user.Password != oldPassword) { ModelState.AddModelError("OldPassword", "原密码错误"); } else { string newPassword = SecurityPsw.SHA1PAssword(model.NewPassword); user.Password = newPassword; db.SaveChanges(); MessageBox.Show("修改密码成功,请重新登陆!"); AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); return(RedirectToAction("Login", "Account")); } return(View()); }