1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23using System . Linq ;
34using System . Text . RegularExpressions ;
45using System . Threading . Tasks ;
@@ -188,43 +189,48 @@ protected virtual void CheckErrors(IdentityResult identityResult)
188189
189190 public async Task < bool > ChangePassword ( ChangePasswordDto input )
190191 {
191- if ( _abpSession . UserId == null )
192+ await _userManager . InitializeOptionsAsync ( AbpSession . TenantId ) ;
193+
194+ var user = await _userManager . FindByIdAsync ( AbpSession . GetUserId ( ) . ToString ( ) ) ;
195+ if ( user == null )
192196 {
193- throw new UserFriendlyException ( "Please log in before attemping to change password. ") ;
197+ throw new Exception ( "There is no current user! ") ;
194198 }
195- long userId = _abpSession . UserId . Value ;
196- var user = await _userManager . GetUserByIdAsync ( userId ) ;
197- var loginAsync = await _logInManager . LoginAsync ( user . UserName , input . CurrentPassword , shouldLockout : false ) ;
198- if ( loginAsync . Result != AbpLoginResultType . Success )
199+
200+ if ( await _userManager . CheckPasswordAsync ( user , input . CurrentPassword ) )
199201 {
200- throw new UserFriendlyException ( "Your 'Existing Password' did not match the one on record. Please try again or contact an administrator for assistance in resetting your password." ) ;
202+ CheckErrors ( await _userManager . ChangePasswordAsync ( user , input . NewPassword ) ) ;
201203 }
202- if ( ! new Regex ( AccountAppService . PasswordRegex ) . IsMatch ( input . NewPassword ) )
204+ else
203205 {
204- throw new UserFriendlyException ( "Passwords must be at least 8 characters, contain a lowercase, uppercase, and number." ) ;
206+ CheckErrors ( IdentityResult . Failed ( new IdentityError
207+ {
208+ Description = "Incorrect password."
209+ } ) ) ;
205210 }
206- user . Password = _passwordHasher . HashPassword ( user , input . NewPassword ) ;
207- CurrentUnitOfWork . SaveChanges ( ) ;
211+
208212 return true ;
209213 }
210214
211215 public async Task < bool > ResetPassword ( ResetPasswordDto input )
212216 {
213217 if ( _abpSession . UserId == null )
214218 {
215- throw new UserFriendlyException ( "Please log in before attemping to reset password." ) ;
219+ throw new UserFriendlyException ( "Please log in before attempting to reset password." ) ;
216220 }
217- long currentUserId = _abpSession . UserId . Value ;
218- var currentUser = await _userManager . GetUserByIdAsync ( currentUserId ) ;
221+
222+ var currentUser = await _userManager . GetUserByIdAsync ( _abpSession . GetUserId ( ) ) ;
219223 var loginAsync = await _logInManager . LoginAsync ( currentUser . UserName , input . AdminPassword , shouldLockout : false ) ;
220224 if ( loginAsync . Result != AbpLoginResultType . Success )
221225 {
222226 throw new UserFriendlyException ( "Your 'Admin Password' did not match the one on record. Please try again." ) ;
223227 }
228+
224229 if ( currentUser . IsDeleted || ! currentUser . IsActive )
225230 {
226231 return false ;
227232 }
233+
228234 var roles = await _userManager . GetRolesAsync ( currentUser ) ;
229235 if ( ! roles . Contains ( StaticRoleNames . Tenants . Admin ) )
230236 {
@@ -235,7 +241,7 @@ public async Task<bool> ResetPassword(ResetPasswordDto input)
235241 if ( user != null )
236242 {
237243 user . Password = _passwordHasher . HashPassword ( user , input . NewPassword ) ;
238- CurrentUnitOfWork . SaveChanges ( ) ;
244+ await CurrentUnitOfWork . SaveChangesAsync ( ) ;
239245 }
240246
241247 return true ;
0 commit comments