internal async System.Threading.Tasks.Task <IdentityResult> ChangePasswordAsync(int userId, string newPassword) { AppUser user = await this.FindByIdAsync(userId).ConfigureAwait(false); if (user == null) { throw new InvalidOperationException("user not found"); } IdentityResult result = await this.PasswordValidator.ValidateAsync(newPassword).ConfigureAwait(false); IUserPasswordStore <AppUser, int> pwdStore = (IUserPasswordStore <AppUser, int>) this.Store; if (result.Succeeded) { await pwdStore.SetPasswordHashAsync(user, newPassword).ConfigureAwait(false); } if (result.Succeeded) { result = await this.UpdateSecurityStampAsync(user.Id); } if (result.Succeeded) { result = await this.UpdateAsync(user); } return(result); }
public void SetPasswordHashAsync() { var user = new UserModel(); var task = userStore.SetPasswordHashAsync(user, "password", cancellationTokenSource.Token); task.Wait(); Assert.AreEqual(user.PasswordHash, "password"); repository.Verify(); repository.VerifyNoOtherCalls(); }
private async Task <IdentityResult> UpdatePasswordHash(IUserPasswordStore <TUser> passwordStore, TUser user, string newPassword, bool validatePassword = true) { var hash = newPassword != null?PasswordHasher.HashPassword(user, newPassword) : null; await passwordStore.SetPasswordHashAsync(user, hash, CancellationToken); // await UpdateSecurityStampInternal(user); return(IdentityResult.Success); }
protected override async Task <bool> VerifyPasswordAsync( IUserPasswordStore <ApplicationUser, string> store, ApplicationUser user, string password) { var hash = await store.GetPasswordHashAsync(user); var verifyRes = PasswordHasher.VerifyHashedPassword(hash, password); if (verifyRes == PasswordVerificationResult.SuccessRehashNeeded) { await store.SetPasswordHashAsync(user, PasswordHasher.HashPassword(password)); } return(verifyRes != PasswordVerificationResult.Failed); }
private async Task <IdentityResult> UpdatePasswordInternal(IUserPasswordStore <IdentityUser, long> passwordStore, IdentityUser user, string newPassword) { var result = await PasswordValidator.ValidateAsync(newPassword).ConfigureAwait(false); if (!result.Succeeded) { return(result); } result = null; await passwordStore.SetPasswordHashAsync(user, PasswordHasher.HashPassword(newPassword)).ConfigureAwait(false); //await UpdateSecurityStampInternal(user).ConfigureAwait(false); return(IdentityResult.Success); }
private async Task <IdentityResult> UpdatePasswordInternal(IUserPasswordStore <TUser, TKey> passwordStore, TUser user, string newPassword) { var result = await PasswordValidator.ValidateAsync(newPassword).WithCurrentCulture(); if (!result.Succeeded) { return(result); } await passwordStore.SetPasswordHashAsync(user, PasswordHasher.HashPassword(newPassword)).WithCurrentCulture(); await UpdateSecurityStampInternal(user).WithCurrentCulture(); return(IdentityResult.Success); }
private async Task <IdentityResult> UpdatePasswordHash(IUserPasswordStore <TUser> passwordStore, TUser user, string newPassword, bool validatePassword = true) { if (validatePassword) { var validate = await ValidatePasswordAsync(user, newPassword); if (!validate.Succeeded) { return(validate); } } var hash = newPassword != null?PasswordHasher.HashPassword(user, newPassword) : null; await passwordStore.SetPasswordHashAsync(user, hash, CancellationToken); return(IdentityResult.Success); }
protected override async Task <bool> VerifyPasswordAsync(IUserPasswordStore <CustomUser, int> store, CustomUser user, string password) { var hash = await store.GetPasswordHashAsync(user).WithCurrentCulture(); var pwdResult = PasswordHasher.VerifyHashedPassword(hash, password); if (pwdResult == PasswordVerificationResult.SuccessRehashNeeded) { var newHashedPwd = PasswordHasher.HashPassword(password); await store.SetPasswordHashAsync(user, newHashedPwd); await store.UpdateAsync(user); } return(pwdResult != PasswordVerificationResult.Failed); }
public void SetPasswordHashAsyncStoredProcExists() { try { var userModel = new UserModel() { Id = Guid.NewGuid().ToString(), UserName = It.IsAny <string>(), NormalizedUserName = It.IsAny <string>(), PasswordHash = It.IsAny <string>(), IsActive = true, }; userStoreMethodLookup.SetPasswordHashAsync(userModel, "password", cancellationTokenSource.Token).Wait(); } catch { Assert.Fail(); } }
private async Task <AuthResult> UpdatePasswordHash(IUserPasswordStore <TUser> passwordStore, TUser user, string newPassword, bool validatePassword = true) { if (validatePassword) { AuthResult validate = await ValidatePasswordAsync(user, newPassword); if (!validate.Succeeded) { return(validate); } } string hash = newPassword != null?PasswordHasher.HashPassword(user, newPassword) : null; await passwordStore.SetPasswordHashAsync(user, hash, CancellationToken); await UpdateSecurityStampInternal(user); return(AuthResult.Success); }
/// <summary> /// Updates the user password. /// </summary> /// <param name="passwordStore">Unused implementation of UserPasswordStore.</param> /// <param name="user">User.</param> /// <param name="newPassword">New password in plain text format.</param> /// <returns><see cref="IdentityResult.Failed(string[])"/> if the new password is either <see langword="null"/>, empty, or not valid. Otherwise <see cref="IdentityResult.Success"/>.</returns> /// <exception cref="ArgumentNullException">Thrown when <paramref name="user"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentException">Thrown when <paramref name="newPassword"/> is either <see langword="null"/> or empty.</exception> protected override async Task <IdentityResult> UpdatePassword(IUserPasswordStore <MedioClinicUser, int> passwordStore, MedioClinicUser user, string newPassword) { if (user == null) { throw new ArgumentNullException(nameof(user)); } if (string.IsNullOrEmpty(newPassword)) { throw new ArgumentException($"The {nameof(newPassword)} argument must not be null or empty."); } var result = await PasswordValidator.ValidateAsync(newPassword); if (!result.Succeeded) { return(result); } UserInfo userInfo = UserInfoProvider.GetUserInfo(user.Id); if (userInfo == null) { user.GUID = Guid.NewGuid(); // Don't change the way the passwords are hashed once the app is released in production. user.PasswordHash = UserInfoProvider.GetPasswordHash(newPassword, UserInfoProvider.NewPasswordFormat, user.GUID.ToString()); await passwordStore.SetPasswordHashAsync(user, user.PasswordHash); } else { UserInfoProvider.SetPassword(userInfo, newPassword); user.PasswordHash = ValidationHelper.GetString(userInfo.GetValue("UserPassword"), string.Empty); await UpdateSecurityStampInternalAsync(user); } return(IdentityResult.Success); }
protected async override Task <bool> VerifyPasswordAsync(IUserPasswordStore <MolUser, int> store, MolUser user, string password) { var hash = await store.GetPasswordHashAsync(user).ConfigureAwait(false); if (this.PasswordHasher.VerifyHashedPassword(hash, password) == PasswordVerificationResult.SuccessRehashNeeded) { // Make our new hash hash = PasswordHasher.HashPassword(password); // Save it to the DB await store.SetPasswordHashAsync(user, hash).ConfigureAwait(false); // Invoke internal method to upgrade the security stamp BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic; MethodInfo minfo = typeof(UserManager <MolUser, int>).GetMethod("UpdateSecurityStampInternal", bindingFlags); var updateSecurityStampInternalTask = (Task)minfo.Invoke(this, new[] { user }); await updateSecurityStampInternalTask.ConfigureAwait(false); // Update user await UpdateAsync(user).ConfigureAwait(false); } return(PasswordHasher.VerifyHashedPassword(hash, password) != PasswordVerificationResult.Failed); }