How to replace AddJwtBearer extension in .NET Core 3.0
我有以下代码可以在.NET Core 2.2中进行编译和工作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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中,出现错误:
Error CS1061 'AuthenticationBuilder' does not contain a definition for
'AddJwtBearer' and no accessible extension method 'AddJwtBearer'
accepting a first argument of type 'AuthenticationBuilder' could be
found (are you missing a using directive or an assembly reference?)
当我查看MSFT文档时:
https://docs.microsoft.com/zh-cn/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。
您必须在项目中包含Microsoft.AspNetCore.Authentication.JwtBearer程序包。 适用于Core 3.0及更高版本的3.0.0版本。