private async Task ValidatePasswordInformation(AppUserMutate value, AppUser newUser) { if (value.CurrentPassword != value.NewPassword) { this.ModelState.AddModelError <AppUserMutate>(x => x.NewPassword, "De wachtwoorden zijn niet gelijk aan elkaar."); } else if (String.IsNullOrEmpty(value.CurrentPassword)) { this.ModelState.AddModelError <AppUserMutate>(x => x.CurrentPassword, "Voer een wachtwoord in."); } else { foreach (IPasswordValidator <AppUser> passwordValidator in this._appUserManager.PasswordValidators) { var validationResult = await passwordValidator.ValidateAsync(this._appUserManager, newUser, value.CurrentPassword); if (!validationResult.Succeeded) { foreach (IdentityError identityError in validationResult.Errors) { this.ModelState.AddModelError <AppUserMutate>(x => x.CurrentPassword, identityError.Description); } } } } }
// POST: api/User public async Task <InsertId> Post([FromBody] AppUserMutate value) { AppUser newUser = AppUser.Create(value.UserName, value.Email, this.GetCurrentGroup()); IdentityResult result = await this._appUserManager.CreateAsync(newUser, value.NewPassword); this.EnsureSucceeded(result); return(newUser.Id); }
public async Task <IActionResult> Put(int id, [FromBody] AppUserMutate value) { AppUser currentUser = await this.GetUser(id); if (value.UserName != currentUser.UserName) { this.EnsureNotCurrentUser(id); } if (value.CurrentPassword != null) { await this.ValidatePasswordInformation(value, currentUser); } if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } currentUser.UserName = value.UserName ?? currentUser.UserName; currentUser.Email = value.Email ?? currentUser.Email; IdentityResult result = await this._appUserManager.UpdateAsync(currentUser); if (!result.Succeeded) { this.ModelState.AppendIdentityResult(result, _ => nameof(value.UserName)); return(this.BadRequest(this.ModelState)); } if (value.NewPassword != null) { if (this.User.Identity.GetUserId() == id) { if (value.CurrentPassword == null) { throw new HttpStatusException(HttpStatusCode.BadRequest); } result = await this._appUserManager.ChangePasswordAsync(currentUser, value.CurrentPassword, value.NewPassword); } else { throw new HttpStatusException(HttpStatusCode.NotImplemented); //result = await this._appUserManager.ChangePasswordAsync(currentUser, value.NewPassword); } if (!result.Succeeded) { this.ModelState.AppendIdentityResult(result, _ => nameof(value.NewPassword)); return(this.BadRequest(this.ModelState)); } } return(this.NoContent()); }
public async Task <IActionResult> Post([FromBody] AppUserMutate value) { AppUser newUser = AppUser.Create(value.UserName, value.Email, this.GetCurrentGroup()); await this.ValidatePasswordInformation(value, newUser); if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } IdentityResult result = await this._appUserManager.CreateAsync(newUser, value.NewPassword); if (!result.Succeeded) { this.ModelState.AppendIdentityResult(result, _ => nameof(value.UserName)); return(this.BadRequest(this.ModelState)); } return(this.CreatedAtRoute("User-Get", new { id = newUser.Id }, await this.Get(newUser.Id))); }
// PUT: api/User/5 public async Task <InsertId> Put(int id, [FromBody] AppUserMutate value) { AppUser currentUser = await this.GetUser(id); if (value.UserName != null) { this.EnsureNotCurrentUser(id); } currentUser.UserName = value.UserName ?? currentUser.UserName; currentUser.Email = value.Email ?? currentUser.Email; IdentityResult result = await this._appUserManager.UpdateAsync(currentUser); this.EnsureSucceeded(result); if (value.NewPassword != null) { if (this.User.Identity.GetUserId() == id.ToString(CultureInfo.CurrentCulture)) { if (value.CurrentPassword == null) { throw new HttpResponseException(HttpStatusCode.BadRequest); } result = await this._appUserManager.ChangePasswordAsync(currentUser.Id, value.CurrentPassword, value.NewPassword); } else { result = await this._appUserManager.ChangePasswordAsync(currentUser.Id, value.NewPassword); } this.EnsureSucceeded(result); } return(id); }