本文介绍了如何在ASP.NET MVC控制器中获取Identity PasswordOptions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从MVC Controller
中读取在Startup.cs
中配置的标识PasswordOptions
.我的PasswordOptions
配置如下:
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;
});
然后如何读取Controller
或应用程序其他位置的RequireDigit
,PasswordLength
和RequireNonAlphanumeric
属性?
How do I then read the RequireDigit
, PasswordLength
, and RequireNonAlphanumeric
properties in a Controller
or elsewhere in the app?
使用ASP.NET Core 1.0.1.
Using ASP.NET Core 1.0.1.
推荐答案
只需将IOptions<IdentityOptions>
接口注入到任何类或控制器的构造函数中,如下所示:
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;
}
}
这篇关于如何在ASP.NET MVC控制器中获取Identity PasswordOptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!