猿问

无法在 .NET Core 3.0 中获得基于 JWT 的安全性

我应该能够使用以下内容。

services.AddAuthentication()
  .AddIdentityServerJwt();

这不起作用,因为AddIdentityServerJwt似乎不存在。另外,我不确定此时此刻我是否想与 IdentityServer 携手并进。


在以前的版本中,我会声明一个默认方案并在 Startup.cs 文件中配置令牌验证。然而现在,似乎所有的奶酪都被转移到了 Core 3 中。

我应该如何使用 JWT 配置(尽可能简单的)安全性并(最好)避免 Identity Server?

public void ConfigureServices(IServiceCollection services)

{

  ...

  services.AddAuthentication();

}


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

{

  ...

  //app.UseRouting();

  app.UseAuthentication();

  //app.UseAuthorization();

  //app.UseEndpoints(e => e.MapControllers());

}

这会在 Swagger 中产生以下错误的不同变体。


未指定authenticationScheme,并且未找到DefaultChallengeScheme。


繁星coding
浏览 134回答 1
1回答

人到中年有点甜

缺少的部分是,在调用 后AddAuthentication,您还需要显式“选择”加入 JWT 承载:public void ConfigureServices(IServiceCollection services){   ...   services     .AddAuthentication()     .AddJwtBearer(); }目前,该操作需要手动安装扩展方法,如[此处](Install-Package Microsoft.AspNetCore.Authentication.JwtBearer -Version 3.0.0-preview6.19307.2)。安装包 Microsoft.AspNetCore.Authentication.JwtBearer -版本 3.0.0-preview6.19307.2Core 3.0 涵盖该方法的文档目前重定向到 Core 2.2 的对应文档,并且由于预览状态,可能会随时更新或删除。
随时随地看视频慕课网APP
我要回答