public ActionResult ChangePass(PasswordModel model) { if (ModelState.IsValid) { if (model.NewPassword != model.RepeatPassword) { ModelState.AddModelError("", "Mật khẩu mới không trùng khớp!"); } else { var dao = new UserDao(); string userName = Session["UserName"].ToString(); var user = dao.GetByUsername(userName); var hashpass = new SaltedPassword(); int result = dao.ChangePass(userName, hashpass.EncodePassword(model.OldPassword, user.SaltPass), hashpass.EncodePassword(model.RepeatPassword, user.SaltPass)); if (result == 1) { return(RedirectToAction("Index", "Login")); } else { ModelState.AddModelError("", "Mật khẩu đăng nhập sai!"); } } } else { ModelState.AddModelError("", "Cập nhật thất bại, vui lòng liên hệ với quản trị viên!"); } return(View()); }
/// <summary> /// Returns a JSON web token if successful, returns null otherwise /// </summary> /// <param name="submittedUser"></param> /// <returns></returns> public async Task <User> Create(UserPost submittedUser) { SaltedPassword password = Auth.GeneratePassword(submittedUser.Password); User user = new User() { HashedPassword = password.HashedPassword, Salt = password.Salt, Username = submittedUser.Username, Person = submittedUser.Person }; if (string.IsNullOrEmpty(submittedUser.Role)) { user.Role = await GetUserRoleId("user"); } else { user.Role = await GetUserRoleId(submittedUser.Role); } if (await UniquePropertyExists(user)) { throw new BadRequestException("User", user.Username); } // Add a person entity to the database and assign that person to the user being added user.Person = await _personService.Create(submittedUser.Person); // Add user to database _context.Users.Add(user); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { if (await UserExists(user.Id)) { throw new ConflictException("User", user.Id.ToString()); } else { throw; } } return(user); }
public ActionResult Login(LoginModel model) { if (ModelState.IsValid) { var dao = new UserDao(); var hashpass = new SaltedPassword(); var user = dao.GetByUsername(model.UserName); if (user != null) { var result = dao.Login(model.UserName, hashpass.EncodePassword(model.Password, user.SaltPass)); if (result == 1) { var userSession = new UserLogin(); userSession.UserName = user.UserName; userSession.UserId = user.ID; userSession.GroupID = user.GroupID; Session["UserName"] = user.UserName; var listCredentials = dao.GetListCredential(model.UserName); Session.Add(CommonFunction.SESSION_CREDENTIALS, listCredentials); Session.Add(CommonFunction.USER_SESSION, userSession); return(RedirectToAction("Index", "Home")); } else if (result == -1) { ModelState.AddModelError("", "Tài khoản đang bị khóa!"); } else { ModelState.AddModelError("", "Đăng nhập không đúng!"); } } else { ModelState.AddModelError("", "Tài khoản không tồn tại!"); } } return(View("Index")); }
public static user Register(string email, string password, SampleDBContext DBContext = null) { if (DBContext == null) { DBContext = new SampleDBContext(); } SaltedPassword saltedPassword = EncryptoService.GenerateSaltedHash(64, password); user newUser = new user() { email = email, salt = saltedPassword.Salt, password = saltedPassword.Hash, created_at = DateTime.Now }; try { DBContext.users.Add(newUser); DBContext.SaveChanges(); } catch (Exception ex) { throw new Exception(ex.ToString()); } return(newUser); }