【发布时间】:2020-02-23 20:32:12
【问题描述】:
我有以下在 .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 指令或程序集引用?)
并尝试升级到 3.0 版本,这似乎是定义它的最后一个版本。如何将 AddJwtBearer 迁移到 Core 3.0?
【问题讨论】:
-
分享你的csproject文件
-
你解决了这个问题吗?在 .net 5 中它说 AddTokenAuthentication 不存在
-
使用接受的答案解决了,但那是 .net core 3.0 你可能需要 net 5 的更新版本,我不使用 .net 5
-
如果您从版本 2.2 转换为 3.0 安装包 Microsoft.AspNetCore.Authentication.JwtBearer -Version 3.0.3
标签: c# asp.net-core .net-core asp.net-core-2.0 .net-core-3.0