Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get Identity PasswordOptions in ASP.NET MVC controller

I want to read the Identity PasswordOptions that are configured in Startup.cs from an MVC Controller. My PasswordOptions is configured like this:

services.AddIdentity<ApplicationUser, IdentityRole>(config => {
        config.Password.RequireDigit = true;
        config.Password.RequiredLength = 8;
        config.Password.RequireNonAlphanumeric = true;
});

How do I then read the RequireDigit, PasswordLength, and RequireNonAlphanumeric properties in a Controller or elsewhere in the app?

Using ASP.NET Core 1.0.1.

like image 284
kindohm Avatar asked Dec 15 '22 01:12

kindohm


1 Answers

Simply inject IOptions<IdentityOptions> interface to any class or controller's constructor like this:

 public class MyController : Controller
 {
     private readonly IOptions<IdentityOptions> _identityOptions;
     public MyContoller(IOptions<IdentityOptions> identityOptions)
     {
         _identityOptions=identityOptions?.Value ?? new IdentityOptions();         
     }

     public MyAction()
     {
         var length=_identityOptions.Value.Password.RequiredLength;
     }
 }
like image 64
Sam FarajpourGhamari Avatar answered Mar 01 '23 23:03

Sam FarajpourGhamari