[英]Pass and instance to a method with Action as parameter
我有以下方法:
IdentityBuilder IServiceCollection.AddIdentityCore<User>(Action<IdentityOptions> setupAction)
我使用如下:
services.AddIdentityCore<User>(x => {
x.Password.RequiredLength = 8;
})
這可行,但我嘗試使用默認值創建 class:
public class IdentityDefaultOptions : IdentityOptions {
public IdentityDefaultOptions() {
Password.RequiredLength = 8;
}
}
並按如下方式使用它:
services.AddIdentityCore<User>(x => new IdentityOptions())
它編譯但 Password.RequiredLength 未應用。
我錯過了什么?
function
services.AddIdentityCore<User>(x => {
x.Password.RequiredLength = 8;
})
不返回任何值,它會更改作為參數到達的x
的值。
您必須獲取一個IdentityOptions
參數並更改傳遞的 object。
public void DefaultIdentityOptions(IdentityOptions x)
{
x.Password.RequiredLength = 8;
}
你為什么要嘗試創建不同的類型?
如果您嘗試在多個位置配置IdentityOptions
實例,或者使用依賴注入對此配置做出其他決策。 您可以改為配置實現IConfigureOptions<IdentityOptions>
的服務;
public class MoreIdentityOptions : IConfigureOptions<IdentityOptions>{
public MoreIdentityOptions(/* inject types */){}
public void Configure(IdentityOptions options){
x.Password.RequiredLength = 8;
}
}
services.AddIdentityCore<User>();
services.AddTransient<IConfigureOptions<IdentityOptions>, MoreIdentityOptions>();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.