How to use ConfigurationManager? (Microsoft.IdentityModel.Protocols)

不羁岁月 提交于 2020-05-12 18:42:46

问题


I was recently forced to update my System.IdentityModel.Tokens.Jwt NuGet package to 5.1.4 because of another NuGet package. Most of the code after changes seem easy enough to solve, but now ConfigurationManager<OpenIdConnectConfiguration>() takes two arguments instead of one! I can not find any example of how to use this new version of the Configuration manager!

I use it as part of this code:

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;

Can anyone let me know what arguments ConfigurationManager expects


回答1:


I found that in order to make ConfigurationManager work with version >=5.1.4 of the System.IdentityModel.Tokens.Jwt NuGet package you have to add OpenIdConnectConfigurationRetriever() as the second argument.

The correct invocation of ConfigurationManager is then:

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



回答2:


Depending on what you want to do, you could just change the code to make a call to the Configuration retriever, like this:

        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
                }
        });


来源:https://stackoverflow.com/questions/45500752/how-to-use-configurationmanager-microsoft-identitymodel-protocols

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!