i am using the following code in configureservices in startup.cs in netcore 3
Can someone show how i can do this a better way, how do i inject it into configure method? I need to access the database before dependency injection has resolved it but buildservices isnt recommended.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<bookingsstrathContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MyEntities2")));
services.AddScoped<IMyAccountService, MyAccountService>();
var sp = services.BuildServiceProvider();
// Resolve the services from the service provider
var _myAccountService = sp.GetService<IMyAccountService>();
//get the local role from the local database
if (_myAccountService.IsUserInRole(context.Principal.GetUserGraphSamAccountName(), "Administrator"))
{
//myIdentity.AddClaim(new Claim(ClaimTypes.Role, "Administrator"));
context.Principal.AddUserSystemRole("Administrator");
}
}