public async Task <IActionResult> Register(RegisterModel model, [FromServices] IGenPasswordHash genPassHash) { if (ModelState.IsValid) { Author user = await db.Authors.FirstOrDefaultAsync(u => (u.Nickname == model.Nickname || u.Email == model.Email)); if (user == null) { // добавляем пользователя в бд string passwordHash = genPassHash.GenerateHash(model.Password); db.Authors.Add(new Author { Nickname = model.Nickname, Email = model.Email, PasswordHash = passwordHash }); await db.SaveChangesAsync(); await Authenticate(model.Nickname, false); // аутентификация return(RedirectToAction("GetIndex", "Home")); } else { ModelState.AddModelError("", "Пользователь с данным email или nickname уже существует"); } } return(View(model)); }
public async Task <IActionResult> Create([FromServices] IGenPasswordHash genPassHash, RegisterModel regModel) { var dbUser = await db.AdministratorUsers.SingleOrDefaultAsync(e => e.Login == regModel.Login); if (dbUser != null) { ModelState.AddModelError("loginError", "name taken"); } if (!ModelState.IsValid) { return(View(regModel)); } string passwordHash = genPassHash.GenerateHash(regModel.Password); db.AdministratorUsers.Add(new AdministratorUser { Login = regModel.Login, PasswordHash = passwordHash, Tier = regModel.Tier }); await db.SaveChangesAsync(); await logger.AddToLogAsync($"Added new account LOGIN:{regModel.Login}"); return(RedirectToAction("Accounts")); }
/// <summary> /// Add new user. /// </summary> /// <param name="model">User to be added</param> /// <returns>Added user data with correct key(Id) value</returns> public async Task <UserData> AddDataModelAsync(RegistrationModel model) { var userData = new UserData { Login = model.Login, RoleName = model.RoleName }; var userAuth = new UserAuth { Login = model.Login, PasswordHash = genPasswordHash.GenerateHash(model.Password) }; var entity = db.Add(userData); db.Add(userAuth); await db.SaveChangesAsync(); return(entity.Entity); }