。NET Core 3.0中如何替换AddJwtBearer扩展名

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

我有以下代码可以在.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中,我得到了错误:

错误CS1061'AuthenticationBuilder'不包含针对“ AddJwtBearer”和没有可访问的扩展方法“ AddJwtBearer”接受类型为“ AuthenticationBuilder”的第一个参数可能是找到(您是否缺少using指令或程序集引用?)

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

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

c# asp.net-core .net-core asp.net-core-2.0 .net-core-3.0
2个回答
0
投票

您必须在项目中包含Microsoft.AspNetCore.Authentication.JwtBearer软件包。


0
投票

就像Mert Sayin所说,包括软件包Microsoft.AspNetCore.Authentication.JwtBearer,但请使用版本3.0.0

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