c# - JWT Authentication 参数错误
问题描述
我正在学习如何将 JWT 令牌身份验证添加到我的 webApi。这是我到目前为止所做的Startup.cs
:
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
..
..
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
AutomaticAuthenticate = true,
TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = "http://localhost:Port",
ValidateAudience = false,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("testPass"))
}
});
app.UseMvc();
}
但我收到如下错误:
JwtBearerAppBuilderExtensions.UseJwtBearerAuthentication(IApplicationBuilder, JwtBearerOptions)' is obsolete: 'See https://go.microsoft.com/fwlink/?linkid=845470
和
2. JwtBearerOptions' does not contain a definition for 'AutomaticAuthenticate'
解决方案
您可以在 中的ConfigureServices
方法中执行此操作StartUp.cs
。
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(o =>
{
o.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = false,
ValidateLifetime = false,
ValidateIssuerSigningKey = true,
ValidIssuer = "http://localhost:Port",
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("YourKey"))
};
});
// other configuration...
}
然后在Configure
方法中:
app.UseAuthentication();
推荐阅读
- xml - 使用 powershell 将一个文件中的新 XML 节点附加或添加到另一个文件
- php - 与自己的数组相同的数组索引
- virtualenv - 如何在 WSL2 上设置自定义 jupyter 内核?
- loadrunner - 分配具有 4 个内核和 4 GB RAM 的负载运行器用户
- php - 提取 ID3 音频标签时允许使用 unicode
- .net - 如何在 .net 应用程序中的 SmtpMail.Send(message) 方法之前验证电子邮件 ID
- python - NetworkX 圆形布局增加节点间距和自定义定位
- react-native - 反应导航:使用嵌套导航器时无限重新加载主屏幕?
- python - 使用线程模块发送邮件时了解 Gunicorn 工作进程
- remote-access - 错误请求 - 使用 VPN 时主机名无效 IIS express