如何在.NET Core 3.0中替换AddJwtBearer扩展


19

我有以下代码可以在.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/zh-cn/dotnet/api/microsoft.extensions.dependencyinjection.jwtbearerextensions.addjwtbearer ? view = aspnetcore-2.2

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


共享您的csproject文件
JeePakaJP

Answers:



By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.