身份服务器离线时验证access_token

时间:2019-06-04 19:30:22

标签: asp.net-core-2.0 identityserver4 asp.net-core-identity

我已经安装了IdentityServer4。据我了解,需要保护的webapi从IdentityServer4获取公共密钥,并使用该密钥来验证JWT的签名密钥。我似乎找不到任何描述公开密钥请求频率的文档。每次验证都要求吗?它是否缓存在需要验证的Web API上?

我可以为公钥应用某种形式的缓存吗,或者这会自动发生?

对于Web API,我使用标准的.NET Core身份来设置对承载的验证:

        services.AddAuthentication("JWT")
                .AddJwtBearer("JWT", options =>
                {
                    options.Authority = "https://identityserver4.tld";
                    options.RequireHttpsMetadata = false;
                    options.Audience = "webapi";
                });

似乎我可以在这里使用一些代码:https://devblogs.microsoft.com/aspnet/jwt-validation-and-authorization-in-asp-net-core/

var tokenValidationParameters = new TokenValidationParameters
{
    ValidateIssuerSigningKey = true,
    ValidateIssuer = true,
    ValidIssuer = "http://localhost:5000/",
    IssuerSigningKey = new X509SecurityKey(new X509Certificate2(certLocation)),
};

app.UseJwtBearerAuthentication(new JwtBearerOptions()
{
    Audience = "http://localhost:5001/", 
    AutomaticAuthenticate = true,
    TokenValidationParameters = tokenValidationParameters
});

这将为我提供本地公钥,但是仍然:不使用上述tokenValidationParameters时多久获取一次公钥?

1 个答案:

答案 0 :(得分:1)

ASP.Net Core中的默认身份验证中间件将在您的options.Authority uri上调用发现终结点,并将缓存身份提供者指定的公共密钥(以及其他配置信息)。当前在首次进行身份验证时进行缓存。上次我检查时,在内存缓存中被用来存储身份提供者的配置(例如公共密钥)。

类似的情况目前发生在PostConfigure(...) function here中。

显然,从理论上讲,您可以根据source codeJwtBearerOptions中提供以下接口的实现,从而自己对身份提供者配置进行管理。

    /// <summary>
    /// Responsible for retrieving, caching, and refreshing the configuration from metadata.
    /// If not provided, then one will be created using the MetadataAddress and Backchannel properties.
    /// </summary>
    public IConfigurationManager<OpenIdConnectConfiguration> ConfigurationManager { get; set; }

默认实现使用OpenIdConnectConfigurationRetriever,您可以为其找到源代码here