C
C#2y ago
PatriQ

❔ AddAuthentication configured dynamically per request within middleware

Hey. I've got a list of tenants in appsettings.json, which can be updated at runtime. I was wondering, if its possible to somehow move this code to a custom middleware, so configuration of authentication can be dynamically updated at runtime.
var tenants = builder.Configuration.GetSection("Tenants").Get<List<Tenant>>();
foreach (var tenant in tenants)
{
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, o =>
{
o.MetadataAddress = tenant.MetadataAddress;
o.RequireHttpsMetadata = tenant.HTTPS;
o.Authority = tenant.Authority;
o.Audience = "account";
o.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = tenant.Issuer,
ValidateLifetime = true,
ClockSkew = TimeSpan.FromMinutes(0)
};
});
}
var tenants = builder.Configuration.GetSection("Tenants").Get<List<Tenant>>();
foreach (var tenant in tenants)
{
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, o =>
{
o.MetadataAddress = tenant.MetadataAddress;
o.RequireHttpsMetadata = tenant.HTTPS;
o.Authority = tenant.Authority;
o.Audience = "account";
o.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = tenant.Issuer,
ValidateLifetime = true,
ClockSkew = TimeSpan.FromMinutes(0)
};
});
}
1 Reply
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server