Nicolas Sabaj
Nicolas Sabaj

Reputation: 23

.NET Core EntityFramework Identity - Changing IdentityOptions (not at Startup.cs)

I'm setting the identity options at Startup, calling:

services.AddIdentity<Usuarios, IdentityRole>(options =>
{
    if (usersConfiguration.PasswordLongitud.HasValue)
        options.Password.RequiredLength = usersConfiguration.PasswordLongitud.Value;
    if (usersConfiguration.CantidadIntentosLogin.HasValue)
        options.Lockout.MaxFailedAccessAttempts = usersConfiguration.CantidadIntentosLogin.Value;

    options.Password.RequireLowercase = usersConfiguration.PasswordRequiereMinusculas;
    options.Password.RequireUppercase = usersConfiguration.PasswordRequiereMayusculas;
    options.Password.RequireNonAlphanumeric = usersConfiguration.PasswordRequiereCaracteresRaros;
    options.Password.RequireDigit = usersConfiguration.PasswordRequiereNumeros;
    options.SignIn.RequireConfirmedEmail = usersConfiguration.UsuarioRequiereConfirmacionMail;
})

I want to let the user of my application to change this options, so what I'm trying to do is changing the identity options from a controller, but I didn't find how to do that. For example, I need to allow the user Administrator to change the RequireLowercase option. If it's possible, I would like to avoid restarting the IIS for calling Startup again.

Sorry for my English.

Upvotes: 2

Views: 569

Answers (1)

jimSampica
jimSampica

Reputation: 12420

By injecting the UserManager into the controller you can access these options and change them. The Identity options are managed through a singleton so all requests will get the updated value.

public MyController(UserManager<User> userManager)
{
    userManager.Options.Password.RequireDigit = true; //Get something from db, config etc.
}

Upvotes: 4

Related Questions