public ActionResult Register(RegisterViewModel registerViewModel) { if (!ModelState.IsValid) { return(View(registerViewModel)); } var user = registerViewModel.GetUser(); var salt = RandomString(128); var hash = PasswordHashing.GetPasswordHash(registerViewModel.Password, salt); var userRole = _roleService.GetRoles(r => r.Name == "user").FirstOrDefault(); user.Roles = new List <Role> { userRole }; user.PasswordSalt = salt; user.PasswordHash = hash; try { _userService.AddUser(user); _userService.Save(); return(RedirectToAction("Login")); } catch { ModelState.AddModelError("", $"User with login {user.Login} already exists!"); return(View(registerViewModel)); } }