public async Task <IActionResult> ChangePhone([FromBody] ChangePhoneDto model) { var user = await this.GetUserByIdentityAsync(_userManager); if (user == null) { return(Unauthorized()); } if (user.PhoneNumber == model.Phone) { return(BadRequest("This phone number is the same as yours")); } user.PhoneNumber = model.Phone; var changePhoneRes = await _userManager.UpdateAsync(user); if (!changePhoneRes.Succeeded) { return(BadRequest(new { Message = "Can't change phone", changePhoneRes.Errors })); } return(Ok(_mapper.Map <UserDto>(user))); }
public async Task <IActionResult> ChangePhone([FromBody] ChangePhoneDto profile, string id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { string userId = User.Claims.First(c => c.Type == "UserID").Value; var user = (Person)await unitOfWork.UserManager.FindByIdAsync(userId); string userRole = User.Claims.First(c => c.Type == "Roles").Value; if (String.IsNullOrEmpty(userRole)) { return(Unauthorized()); } if (user == null) { return(NotFound("User not found")); } var result = await unitOfWork.ProfileRepository.ChangePhone(user, profile.Phone); if (result.Succeeded) { return(Ok(result)); } return(BadRequest(result.Errors)); } catch (Exception) { return(StatusCode(500, "Failed to save changes")); } }
public async Task <IActionResult> ChangePhone([FromBody] ChangePhoneDto dto) { ChangePhoneDtoValidator validator = new ChangePhoneDtoValidator(); ValidationResult result = await validator.ValidateAsync(dto); if (result.IsValid) { #region 驗證重複 if (await _userManager.Users.AnyAsync(x => x.PhoneNumber == dto.NewPhoneNumber)) { result.Errors.Add(new ValidationFailure("newPhoneNumber", "新的手機號碼已經被使用")); return(BadRequest(result.Errors)); } #endregion var userId = User.Claims .Single(p => p.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").Value; var user = await _userManager.FindByIdAsync(userId); await using (var transaction = await _dbContext.Database.BeginTransactionAsync()) { try { user.PhoneNumber = dto.NewPhoneNumber; user.PhoneNumberConfirmed = false; _dbContext.Users.Update(user); if (await _dbContext.SaveChangesAsync() < 0) { throw new DbUpdateException(); } #region UpdateSecurity var oldSecurityStamp = user.SecurityStamp; if (await _userManager.UpdateSecurityStampAsync(user) != IdentityResult.Success) { throw new DbUpdateException(); } if (await _userManager.ReplaceClaimAsync(user, new Claim(ClaimTypes.Sid, oldSecurityStamp), new Claim(ClaimTypes.Sid, user.SecurityStamp)) != IdentityResult.Success) { throw new DbUpdateException(); } #endregion await transaction.CommitAsync(); } catch (DbUpdateException) { await transaction.RollbackAsync(); throw; } } return(NoContent()); } return(BadRequest(result.Errors)); }