0

I made a custom PasswordValidator with its own set of options:

class MyValidator : Identity.PasswordValidator<TUser>
{
    Task<IdentityResult> ValidateAsync(UserManager<TUser> manager, TUser user, string password)
    {
        var options = // retrieve options
        if (options.CustomRule.Matches(password))
        // etc
    }
}

class MyPasswordOptions : Identity.PasswordOptions
{
    object CustomRule { get; set; }
}

Now I want to access an instance of MyPasswordOptions in MyValidator. I can, for example:

  • simply register this class in Startup.cs like any options class, and inject it in the constructor of MyValidator;
  • pass an instance of the MyPasswordOptions class to the options.Password property in services.AddIdentity(), and inject IdentityOptions in the constructor of MyValidator.

Microsoft's original implementation however accesses the options through an internal property of UserManager, which seems like the best option to me, if it wasn't internal.

Question: What is Microsoft's intended way to access the options in Asp Identity Core in a custom implementation? Say, I did not have a custom options class. I still could not easily access the IdentityOptions in the MyValidator class, without injecting them. While the original class (Identity.PasswordValidator) could.

Nick Muller
  • 2,003
  • 1
  • 16
  • 43

1 Answers1

1

In the source code it is retrieved via UserManager:

manager.Options.Password

This works for getting default options, for custom options i think you need to register and resolve it.

See source code

adem caglin
  • 22,700
  • 10
  • 58
  • 78
  • Sadly Options is an internal property. It would have worked for custom options if I cast it to my own class. – Nick Muller Sep 14 '16 at 10:15
  • 1
    Oh i see, you are right. But even if it was possible you couldn't cast it to your own class. Because you couldn't cast base class to derived class. It seems you have to inject options. I would use separete custom options instead of derived. – adem caglin Sep 14 '16 at 11:20
  • @ademcaglin How can I inject my custom `IdentityOptions`? There's no `.AddIdentityOptions()` extension method on `UserManager`! – TheMah Aug 25 '21 at 13:47