Migration To NET6

Viewed 4039

I tried to migrate my ASP CORE project to NET6 My project uses next packages

IdentityServer4.AccessTokenValidation - 3.0.1

IdentityModel.AspNetCore.OAuth2Introspection - 4.0.1

IdentityModel - 5.2.0

The build of project is success. But when I run application I get error

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()

Anybody had this issue ?

3 Answers

I investigated this problem and found cause. I used IdentityModel V 4,2,2 before update. When I update my project to NET 6, IdentityModel was upgrated to version 5.2.0. The difference between IdentityModel V 4,2,2 and IdentityModel version 5.2.0 was in signature method.

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

So we see that in new wersion default parameter was added. But this method is called by method from IdentityServer4.AccessTokenValidation package. And this package was not compiled to call updated ParseUrl function.

See this topic about this problem c# method with default parameter value does not generate overload without parameter?

The Roman's answer is correctly and we can fix it doing the IdentityModel downgrade, but another way to fix that issue is replacing the IdentityServer4.AccessTokenValidation by Microsoft.AspNetCore.Authentication.JwtBearer and we can change a litle bit the token validation, using IdentityServer4.AccessTokenValidation we were doing the validation like that:

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

Now we can do the token validation using the Microsoft.AspNetCore.Authentication.JwtBearer like that:

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"] };
    });
Related