ASP.NET Core Identity?
Is there a way here I can check if the token has expired or if it already has been used? If so how do I set the expiration time?
/// <summary>
/// Handles the logic for resetting a user's password based on the provided command.
/// </summary>
public class ResetPasswordHandler(UserManager<User> userManager)
: IRequestHandler<ResetPasswordCommand, Result<bool>>, IDisposable
{
public void Dispose()
{
userManager.Dispose();
GC.SuppressFinalize(this);
}
public async Task<Result<bool>> Handle(ResetPasswordCommand request, CancellationToken cancellationToken)
{
// Validate the input using the ResetPasswordValidator.
var validator = new ResetPasswordValidator();
var validationResult = await validator.ValidateAsync(request, cancellationToken);
if (!validationResult.IsValid)
return Result<bool>.Failure("Invalid input received.", 400);
// Find the user by email address.
var user = await userManager.FindByEmailAsync(request.Email);
if (user == null)
return Result<bool>.Failure("User not found.", 404);
// Reset the user's password.
var result = await userManager.ResetPasswordAsync(user, request.Token, request.Password);
// Return the appropriate result based on the reset operation success.
return !result.Succeeded
? Result<bool>.Failure("Unable to reset password.", 500)
: Result<bool>.Success(true);
}
}
/// <summary>
/// Handles the logic for resetting a user's password based on the provided command.
/// </summary>
public class ResetPasswordHandler(UserManager<User> userManager)
: IRequestHandler<ResetPasswordCommand, Result<bool>>, IDisposable
{
public void Dispose()
{
userManager.Dispose();
GC.SuppressFinalize(this);
}
public async Task<Result<bool>> Handle(ResetPasswordCommand request, CancellationToken cancellationToken)
{
// Validate the input using the ResetPasswordValidator.
var validator = new ResetPasswordValidator();
var validationResult = await validator.ValidateAsync(request, cancellationToken);
if (!validationResult.IsValid)
return Result<bool>.Failure("Invalid input received.", 400);
// Find the user by email address.
var user = await userManager.FindByEmailAsync(request.Email);
if (user == null)
return Result<bool>.Failure("User not found.", 404);
// Reset the user's password.
var result = await userManager.ResetPasswordAsync(user, request.Token, request.Password);
// Return the appropriate result based on the reset operation success.
return !result.Succeeded
? Result<bool>.Failure("Unable to reset password.", 500)
: Result<bool>.Success(true);
}
}
0 Replies