我有以下代码可以在.NET Core 2.2中进行编译和工作:

  byte[] key = Encoding.ASCII.GetBytes(Constants.JWT_SECRET);
        services.AddAuthentication(x =>
        {
            x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        })
        .AddJwtBearer(x =>
        {
            x.RequireHttpsMetadata = false;
            x.SaveToken = true;
            x.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuerSigningKey = true,
                IssuerSigningKey = new SymmetricSecurityKey(key),
                ValidateIssuer = false,
                ValidateAudience = false
            };
        });

在.NET Core 3.0中,出现错误:



当我查看MSFT文档时:
https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.jwtbearerextensions.addjwtbearer?view=aspnetcore-2.2

并尝试升级到3.0版,这似乎是定义此版本的最后一个版本。如何将AddJwtBearer迁移到Core 3.0?

最佳答案

就像Mert Sayin所说的那样,包括Microsoft.AspNetCore.Authentication.JwtBearer软件包,但是要使用版本3.0.0

09-27 06:03