I have an ASP.NET Core 3 application, and I'm using AzureAD for authentication. I have the following lines in my Startup.ConfigureSerivces method, whose purpose is to perform some business rules when the cookie is appended.
services.Configure<CookiePolicyOptions>(options => {
options.CheckConsentNeeded = ctx => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
options.OnAppendCookie = ctx => {
var svc = ctx.Context.RequestServices.GetRequiredService<IUserInformationService>();
// do something with svc
};
});
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => {
Configuration.Bind("AzureAd", options);
});
This works well, and I get the injected IUserInformationService
object as expected in the OnAppendCookie
method and the AzureAD info is taken from appsettings.json.
Recently however, this information about the AzureAD tenant must not reside in appsettings.json, but rather I now must consult a database. I have a service that already queries the database and gets the AD settings. Something like:
public interface IAzureADConfiguration {
void Configure(AzureADOptions options);
}
However, I can't find a way to retrieve the injected service when calling AddAzureAD
. What I want is something like this:
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => {
var svc = ???
svc.Configure(options);
Configuration.Bind("AzureAd", options);
});
Since I have no access to HttpContext
in AddAzureAD
method as I did in OnAppendCookie
. Is there a way to get injected objects at this stage? I don't want to hard-code an instance because this requirement is likely to change in the future (i.e. add another mean to configure where I get the Azure Ad settings). Thanks in advance!