Create README.md
This commit is contained in:
parent
64b95fdff3
commit
9af8cd8a8a
79
README.md
Normal file
79
README.md
Normal file
@ -0,0 +1,79 @@
|
||||
# Allows to have a dynamic authority at runtime
|
||||
|
||||
With the help of changing the JwtHandler by a service, that allows you to define the openidconfiguration/multiple ones at runtime.
|
||||
|
||||
|
||||
# How to use
|
||||
|
||||
> Startup class
|
||||
|
||||
```csharp
|
||||
public class Startup
|
||||
{
|
||||
private readonly IConfiguration configuration;
|
||||
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddHttpContextAccessor();
|
||||
services.AddMemoryCache();
|
||||
services
|
||||
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddDynamicJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
|
||||
{
|
||||
options.TokenValidationParameters.ValidateAudience = false;
|
||||
})
|
||||
.AddDynamicAuthorityJwtBearerResolver<ResolveAuthorityService>();
|
||||
|
||||
services.AddControllers();
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
app.UseRouting();
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapControllers();
|
||||
});
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
> Simple authority resolver with header X-Tenant
|
||||
|
||||
```csharp
|
||||
internal class ResolveAuthorityService : IDynamicJwtBearerAuthorityResolver
|
||||
{
|
||||
private readonly IConfiguration configuration;
|
||||
|
||||
public ResolveAuthorityService(IConfiguration configuration)
|
||||
{
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
public TimeSpan ExpirationOfConfiguration => TimeSpan.FromHours(1);
|
||||
|
||||
public Task<string> ResolveAuthority(HttpContext httpContext)
|
||||
{
|
||||
var realm = httpContext.Request.Headers["X-Tenant"].FirstOrDefault() ?? configuration["KeyCloak:MasterRealm"];
|
||||
var authority = $"{configuration["KeyCloak:Endpoint"]}/realms/{realm}";
|
||||
return Task.FromResult(authority);
|
||||
}
|
||||
}
|
||||
```
|
Loading…
Reference in New Issue
Block a user