dynamic authority.
This commit is contained in:
parent
d06df142e8
commit
a8c995cb64
@ -0,0 +1,37 @@
|
|||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
using Microsoft.IdentityModel.Protocols;
|
||||||
|
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace PoweredSoft.DynamicJwtBearer.DynamicAuthority
|
||||||
|
{
|
||||||
|
public class DynamicAuthorityJwtBearerHandlerConfigurationResolver : IDynamicJwtBearerHanderConfigurationResolver
|
||||||
|
{
|
||||||
|
private readonly IMemoryCache memoryCache;
|
||||||
|
private readonly IDynamicJwtBearerAuthorityResolver dynamicJwtAuthorityResolver;
|
||||||
|
|
||||||
|
public DynamicAuthorityJwtBearerHandlerConfigurationResolver(IMemoryCache memoryCache, IDynamicJwtBearerAuthorityResolver dynamicJwtAuthorityResolver)
|
||||||
|
{
|
||||||
|
this.memoryCache = memoryCache;
|
||||||
|
this.dynamicJwtAuthorityResolver = dynamicJwtAuthorityResolver;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<OpenIdConnectConfiguration> ResolveCurrentOpenIdConfiguration(HttpContext context)
|
||||||
|
{
|
||||||
|
var authority = await dynamicJwtAuthorityResolver.ResolveAuthority(context);
|
||||||
|
var cacheKey = $"DynamicAuthorityJwtBearerHandlerConfigurationResolver__{authority}";
|
||||||
|
var ret = await memoryCache.GetOrCreateAsync(cacheKey, async cacheEntry =>
|
||||||
|
{
|
||||||
|
cacheEntry.AbsoluteExpirationRelativeToNow = dynamicJwtAuthorityResolver.ExpirationOfConfiguration;
|
||||||
|
var configurationManager = new ConfigurationManager<OpenIdConnectConfiguration>($"{authority}/.well-known/openid-configuration", new OpenIdConnectConfigurationRetriever());
|
||||||
|
var authorityConfiguration = await configurationManager.GetConfigurationAsync(context.RequestAborted);
|
||||||
|
return authorityConfiguration;
|
||||||
|
});
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace PoweredSoft.DynamicJwtBearer.DynamicAuthority
|
||||||
|
{
|
||||||
|
public interface IDynamicJwtBearerAuthorityResolver
|
||||||
|
{
|
||||||
|
public TimeSpan ExpirationOfConfiguration { get; }
|
||||||
|
|
||||||
|
public Task<string> ResolveAuthority(HttpContext httpContext);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\PoweredSoft.DynamicJwtBearer\PoweredSoft.DynamicJwtBearer.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
@ -0,0 +1,26 @@
|
|||||||
|
using Microsoft.AspNetCore.Authentication;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace PoweredSoft.DynamicJwtBearer.DynamicAuthority
|
||||||
|
{
|
||||||
|
public static class ServiceCollectionExtensions
|
||||||
|
{
|
||||||
|
public static IServiceCollection AddDynamicAuthorityJwtBearerResolver<TAuthoritySolver>(this IServiceCollection services)
|
||||||
|
where TAuthoritySolver : class, IDynamicJwtBearerAuthorityResolver
|
||||||
|
{
|
||||||
|
services.AddTransient<IDynamicJwtBearerAuthorityResolver, TAuthoritySolver>();
|
||||||
|
services.AddSingleton<IDynamicJwtBearerHanderConfigurationResolver, DynamicAuthorityJwtBearerHandlerConfigurationResolver>();
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static AuthenticationBuilder AddDynamicAuthorityJwtBearerResolver<TAuthoritySolver>(this AuthenticationBuilder authenticationBuilder)
|
||||||
|
where TAuthoritySolver : class, IDynamicJwtBearerAuthorityResolver
|
||||||
|
{
|
||||||
|
authenticationBuilder.Services.AddDynamicAuthorityJwtBearerResolver<TAuthoritySolver>();
|
||||||
|
return authenticationBuilder;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.29503.13
|
|||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PoweredSoft.DynamicJwtBearer", "PoweredSoft.DynamicJwtBearer\PoweredSoft.DynamicJwtBearer.csproj", "{0A15F002-66C4-44D1-8162-563F860C49E4}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PoweredSoft.DynamicJwtBearer", "PoweredSoft.DynamicJwtBearer\PoweredSoft.DynamicJwtBearer.csproj", "{0A15F002-66C4-44D1-8162-563F860C49E4}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PoweredSoft.DynamicJwtBearer.DynamicAuthority", "PoweredSoft.DynamicJwtBearer.DynamicAuthority\PoweredSoft.DynamicJwtBearer.DynamicAuthority.csproj", "{61B13BD6-C86E-4698-8E3B-F26508E9658C}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@ -15,6 +17,10 @@ Global
|
|||||||
{0A15F002-66C4-44D1-8162-563F860C49E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{0A15F002-66C4-44D1-8162-563F860C49E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{0A15F002-66C4-44D1-8162-563F860C49E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{0A15F002-66C4-44D1-8162-563F860C49E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{0A15F002-66C4-44D1-8162-563F860C49E4}.Release|Any CPU.Build.0 = Release|Any CPU
|
{0A15F002-66C4-44D1-8162-563F860C49E4}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{61B13BD6-C86E-4698-8E3B-F26508E9658C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{61B13BD6-C86E-4698-8E3B-F26508E9658C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{61B13BD6-C86E-4698-8E3B-F26508E9658C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{61B13BD6-C86E-4698-8E3B-F26508E9658C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
Loading…
Reference in New Issue
Block a user