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

Can't get JWT based security in .NET Core 3.0 working

根据 MSDN,我应该可以使用以下内容。

services.AddAuthentication()
  .AddIdentityServerJwt();

这不起作用,因为 AddIdentityServerJwt 似乎不存在。另外,我完全不确定此时是否要与 IdentityServer 并驾齐驱。

我还没有找到任何讨论基于 .NET Core 3.0 后端的 SPA 安全性的教程或博客 a direct referrer to the link above (hence relying in the Identity Server). Probably because it such a cutting edge tech at the moment. The migration manual from 2.2 to 3.0 is not exhausting and I also suspect that it might be inaccurate

在以前的版本中,我会在 Startup.cs 文件中声明一个默认方案并配置令牌验证。不过现在看来,核心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 中产生以下错误的不同变体。

No authenticationScheme was specified, and there was no DefaultChallengeScheme found.

缺少的部分是在调用 AddAuthentication 之后,您还需要显式 "optin" 进入 JWT 承载:

public void ConfigureServices(IServiceCollection services)
{
  ...
  services
    .AddAuthentication()
    .AddJwtBearer();
}

目前,该操作需要手动安装扩展方法,如[此处]所示(Install-Package Microsoft.AspNetCore.Authentication.JwtBearer - 版本 3.0.0-preview6.19307.2)。

Install-Package Microsoft.AspNetCore.Authentication.JwtBearer -Version 3.0.0-preview6.19307.2

覆盖该方法的 documentation for Core 3.0 目前重定向到 Core 2.2 的对应部分,并且由于处于预览状态,可能随时更新或删除。