如何使用ConfigurationManager? (Microsoft.IdentityModel.Protocols)

问题描述 投票:4回答:2

由于另一个NuGet包,我最近被迫将我的System.IdentityModel.Tokens.Jwt NuGet包更新到5.1.4。更改后的大多数代码似乎都很容易解决,但现在ConfigurationManager<OpenIdConnectConfiguration>()需要两个参数而不是一个!我找不到如何使用这个新版本的配置管理器的任何示例!

我将它用作此代码的一部分:

string stsDiscoveryEndpoint = string.Format("{0}/.well-known/openid-configuration", authority);

ConfigurationManager<OpenIdConnectConfiguration> configManager = new ConfigurationManager<OpenIdConnectConfiguration>(stsDiscoveryEndpoint, IConfigurationRetriever<>);

OpenIdConnectConfiguration config = await configManager.GetConfigurationAsync();
_issuer = config.Issuer;
_signingTokens = config.SigningTokens.ToList();

_stsMetadataRetrievalTime = DateTime.UtcNow;

任何人都可以让我知道ConfigurationManager期望的论点

c# configuration openid-connect
2个回答
8
投票

我发现为了使ConfigurationManagerSystem.IdentityModel.Tokens.Jwt NuGet包的版本> = 5.1.4一起工作,你必须添加OpenIdConnectConfigurationRetriever()作为第二个参数。

然后正确调用ConfigurationManager

ConfigurationManager<OpenIdConnectConfiguration> configManager = new ConfigurationManager<OpenIdConnectConfiguration>(stsDiscoveryEndpoint, new OpenIdConnectConfigurationRetriever());

2
投票

根据您的要求,您可以更改代码以调用Configuration猎函,如下所示:

        string issuerEndpoint = "https://my.auth.server";
        var openidConfiguration = await OpenIdConnectConfigurationRetriever.GetAsync(
                    $"{issuerEndpoint}/.well-known/openid-configuration", CancellationToken.None);

        app.UseJwtBearerAuthentication(
        new Microsoft.Owin.Security.Jwt.JwtBearerAuthenticationOptions()
        {
            TokenValidationParameters =
                new TokenValidationParameters
                {
                    ValidIssuer = openidConfiguration.Issuer,
                    ValidateAudience = false,
                    IssuerSigningKeys = openidConfiguration.SigningKeys,
                    IssuerSigningTokens = openidConfiguration.SigningTokens
                }
        });
© www.soinside.com 2019 - 2024. All rights reserved.