Skip to content

PasswordValidator should not throw when password is null #58133

@vjacquet

Description

@vjacquet

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

The sole purpose of IPasswordValidator is to validate passwords.

The contract allows null password
Task ValidateAsync(UserManager manager, TUser user, string? password);

The implementation of PasswordValidator handle null password correctly:

if (string.IsNullOrWhiteSpace(password) || password.Length < options.RequiredLength)

So why does it throw when the password is null ?

public virtual Task<IdentityResult> ValidateAsync(UserManager<TUser> manager, TUser user, string? password)
{
ArgumentNullThrowHelper.ThrowIfNull(password);

Expected Behavior

Not throw an ArgumentNullException when the password is null, because the contract setup by the IPasswordValidator.ValidateAsync says it is allowed.

Steps To Reproduce

The following test should pass instead of failing with ArgumentNullException

    [Fact]
    public async Task ValidateDoesNotThrowsWithNullPasswordTest()
    {
        // Setup
        var validator = new PasswordValidator<PocoUser>();

        // Act
        var result = await validator.ValidateAsync(default!, default!, null);

        // Assert
        Assert.False(result.Succeeded);
    }

Exceptions (if any)

No response

.NET Version

8.0.400

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-identityIncludes: Identity and providersbugThis issue describes a behavior which is not expected - a bug.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions