how can I get _unitOfWork. I tried it but I get an error
public class Startup
{
public IConfiguration Configuration { get; }
private readonly ILogger _logger;
private readonly IUnitOfWork _unitOfWork;
public Startup(IConfiguration configuration, ILogger<Startup> logger, IUnitOfWork unitOfWork)
{
Configuration = configuration;
_logger = logger;
_unitOfWork = unitOfWork;
}
public void ConfigureServices(IServiceCollection services, IUnitOfWork unitOfWork)
{
...
}
}
InvalidOperationException: Unable to resolve service for type 'Test.Repository.Abstract.IUnitOfWork' while attempting to activate 'Test.WebUI.Startup'.
InvalidOperationException: Unable to resolve service for type 'Test.Repository.Abstract.IUnitOfWork' while attempting to activate 'Test.WebUI.Startup'.
You could resolve the IUnitOfWork like below:
public class Startup { private StaticSetting staticSetting;
public void ConfigureServices(IServiceCollection services)
{
services.Configure<IdentityOptions>(options =>
{
options.Password.RequireDigit = staticSetting.RequireDigit;
//more settings..
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
this.staticSetting = app.ApplicationServices.GetRequiredService<IUnitOfWork>().StaticSettings.GetAll().FirstOrDefault(); //...
} }
Best Regards,
Rena
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
public class CustomIdentityOptions : IOptions<IdentityOptions>
{
public IdentityOptions Value { get; private set; }
public CustomIdentityOptions()
{
Value = new IdentityOptions
{
Lockout = new LockoutOptions
{
AllowedForNewUsers = true,
DefaultLockoutTimeSpan = TimeSpan.FromMinutes(1),
MaxFailedAccessAttempts = 1
}
};
}
}
NullReferenceException: Object reference not set to an instance of an object.
You could configure IdentityOptions like below:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//...
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IOptions<IdentityOptions> identityOptions)
{
this.staticSetting = app.ApplicationServices.CreateScope().ServiceProvider
.GetRequiredService<ApplicationDbContext>().StaticSettings.FirstOrDefault();
identityOptions.Value.Password.RequireDigit = staticSetting.RequireDigit;
}
}
Best Regards,
Rena
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
40 Points
209 Posts
How to set IdentityOptions Dynamically from database
Aug 24, 2019 10:45 AM|suat_suphi|LINK
how to set IdentityOptions Dynamically from database asp.net core 2?
I can set it statically
But I wanna configure it from database. I have some columns about that.
how and where can I set it from database?
All-Star
53554 Points
13305 Posts
Re: How to set IdentityOptions Dynamically from database
Aug 25, 2019 12:32 AM|bruce (sqlwork.com)|LINK
use closure and assignment:
Member
40 Points
209 Posts
Re: How to set IdentityOptions Dynamically from database
Aug 25, 2019 12:55 AM|suat_suphi|LINK
thank you
how can I get _unitOfWork. I tried it but I get an error
InvalidOperationException: Unable to resolve service for type 'Test.Repository.Abstract.IUnitOfWork' while attempting to activate 'Test.WebUI.Startup'.
Participant
1040 Points
347 Posts
Re: How to set IdentityOptions Dynamically from database
Aug 26, 2019 05:21 AM|Rena Ni|LINK
Hi suat_suphi,
You could resolve the IUnitOfWork like below:
Best Regards,
Rena
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
40 Points
209 Posts
Re: How to set IdentityOptions Dynamically from database
Aug 26, 2019 09:19 AM|suat_suphi|LINK
First step is ConfigureServices... so I get this error
NullReferenceException: Object reference not set to an instance of an object.
Inoticed that there is a event for ConfigureApplicationCookie, but there is no event for IdentityOptions
Feature request: https://github.com/aspnet/AspNetCore/issues/13439
I noticed that other one: these settings is necessary for login, register and new password
how can I get or achieve IUnitOfWork in override method ?
services.AddScoped<IOptions<IdentityOptions>, CustomIdentityOptions>();
Participant
1040 Points
347 Posts
Re: How to set IdentityOptions Dynamically from database
Aug 27, 2019 04:49 AM|Rena Ni|LINK
Hi suat_suphi,
You could configure IdentityOptions like below:
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //... } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, IOptions<IdentityOptions> identityOptions) { this.staticSetting = app.ApplicationServices.CreateScope().ServiceProvider .GetRequiredService<ApplicationDbContext>().StaticSettings.FirstOrDefault(); identityOptions.Value.Password.RequireDigit = staticSetting.RequireDigit; } }
Best Regards,
Rena
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
40 Points
209 Posts
Re: How to set IdentityOptions Dynamically from database
Aug 30, 2019 09:32 AM|suat_suphi|LINK
thank you Rena Ni,
I noticed that these settings are necessary for login, register and new password.
so I must use it when it is necessary.
If I use it in startup, I will get a delay. So I change my logic.