迁移到NET6

问题描述 投票:0回答:5

我尝试将我的 ASP CORE 项目迁移到 NET6 我的项目使用下一个包

IdentityServer4.AccessTokenValidation - 3.0.1

IdentityModel.AspNetCore.OAuth2Introspection - 4.0.1

身份模型 - 5.2.0

项目构建成功。 但是当我运行应用程序时,我收到错误

MissingMethodException: Method not found: 'IdentityModel.Client.DiscoveryEndpoint IdentityModel.Client.DiscoveryEndpoint.ParseUrl(System.String)'.
IdentityServer4.AccessTokenValidation.IdentityServerAuthenticationOptions.ConfigureJwtBearer(JwtBearerOptions jwtOptions)
IdentityServer4.AccessTokenValidation.ConfigureInternalOptions.Configure(string name, JwtBearerOptions options)
Microsoft.Extensions.Options.OptionsFactory<TOptions>.Create(string name)
Microsoft.Extensions.Options.OptionsMonitor<TOptions>+<>c__DisplayClass10_0.<Get>b__0()

有人遇到这个问题吗?

identityserver4 .net-6.0
5个回答
17
投票

Roman 的答案是正确的,我们可以通过 IdentityModel 降级来修复它,但解决该问题的另一种方法是将 IdentityServer4.AccessTokenValidation 替换为 Microsoft.AspNetCore.Authentication.JwtBearer,我们可以更改使用 IdentityServer4.AccessTokenValidation 进行一点令牌验证,我们进行如下验证:

services
    .AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
    .AddIdentityServerAuthentication(
        options =>
        {
            options.Authority = configuration["Authentication:Authority"];
            options.ApiName = configuration["Authentication:ApiName"];
        });

现在我们可以使用 Microsoft.AspNetCore.Authentication.JwtBearer 进行令牌验证,如下所示:

services
    .AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    })
    .AddJwtBearer(options =>
    {
        options.Authority = configuration["Authentication:Authority"];
        options.RequireHttpsMetadata = false;
        options.TokenValidationParameters.ValidAudiences = new List<string>() { configuration["Authentication:ApiName"] };
    });

13
投票

我调查了这个问题并找到了原因。 我在更新之前使用了 IdentityModel V 4,2,2。 当我将项目更新到 NET 6 时,IdentityModel 已升级到版本 5.2.0。 IdentityModel V 4,2,2 和 IdentityModel 5.2.0 版本之间的区别在于签名方法。

public static DiscoveryEndpoint ParseUrl(string input, string path = null) version 5,2,0
public static DiscoveryEndpoint ParseUrl(string input) version 4,2,2

所以我们看到在新版本中添加了默认参数。 但是这个方法是由 IdentityServer4.AccessTokenValidation 包中的方法调用的。并且这个包没有被编译来调用更新的 ParseUrl 函数。

有关此问题请参阅此主题带有默认参数值的c#方法不生成无参数重载?


3
投票

IdentityServer4.AccessTokenValidation 库已弃用,因此没有针对 IdentityModel 版本 5.2.0 编译的版本。

您应该使用https://leastprivilege.com/2020/07/06/flexible-access-token-validation-in-asp-net-core/

中描述的新方法

2
投票

我认为你可以尝试使用 IdentityModel.AspNetCore.OAuth2Introspection 6.2.0。当我将 IdentityServer4.AccessTokenValidation 迁移到 .Net 6 Web API 时,它对我有用。

配置:

services.AddAuthentication(OAuth2IntrospectionDefaults.AuthenticationScheme)
    .AddOAuth2Introspection(options =>
    {
        options.Authority = "https://base_address_of_token_service";

        options.ClientId = "client_id_for_introspection_endpoint";
        options.ClientSecret = "client_secret_for_introspection_endpoint";
    })

请在https://github.com/IdentityModel/IdentityModel.AspNetCore.OAuth2Introspection

找到更多详细信息

Nuget:https://www.nuget.org/packages/IdentityModel.AspNetCore.OAuth2Introspection/6.2.0?_src=template


0
投票

它从 IdentityModel 5.1.1 起就不起作用了,从 5.2.0 起就不起作用了

public static DiscoveryEndpoint ParseUrl(string input, string path = null) version 5,1,1
public static DiscoveryEndpoint ParseUrl(string input) version 5,1,0

https://github.com/IdentityModel/IdentityModel/compare/5.1.0...5.1.1#diff-ffcf348c072ed429fd09e5bbc12b85703d3cd92add69f6af131d79b09ea17f30R20

© www.soinside.com 2019 - 2024. All rights reserved.