Search code examples
c#asp.net-coreasp.net-core-2.2asp.net-core-identity

Remove default PasswordValidator from Asp.Net Core


I've created my own PasswordValidator and add it to the IdentityBuilder by using services.AddIdentity<, >().AddPasswordValidator<>(). This adds my PasswordValidator on top of the default one, so the password will be checked by both. (not what I need).

How can I remove the default one?


Solution

  • AddIdentity only adds the default PasswordValidator implementation if one hasn't already been registered. Because of this, you can add your custom implementation before calling AddIdentity:

    services.AddScoped<IPasswordValidator<YourUser>, YourPasswordValidator>();
    services.AddIdentity<YourUser, YourRole>(...);
    

    In this case, only YourPasswordValidator gets registered.