.NET Core 2.0 中 IServiceCollection 缺少扩展方法 AddJwtBearerAuthentication()

Missing extension method AddJwtBearerAuthentication() for IServiceCollection in .NET Core 2.0

我已使用 https://blogs.msdn.microsoft.com/webdev/2017/08/14/announcing-asp-net-core-2-0/ 中的说明将我的项目从 Core 1.1 更新到 Core 2.0 (将目标框架更新为 .NET Core 2.0 并使用元数据包 Microsoft.AspNetCore.All)。我也已将所有可能的 nuget 包更新到最新版本。

在 .NET Core 1.1 中,我以这种方式添加了 JWT Bearer Authentication:

app.UseJwtBearerAuthentication(); // from Startup.Configure()

根据 http://www.talkingdotnet.com/whats-new-in-asp-net-core-2-0/ Core 2.0 的新方法是调用:

services.AddJwtBearerAuthentication(); // from Startup.ConfigureServices()

但是 AddJwtBearerAuthentication() 方法不存在。软件包 Microsoft.AspNetCore.Authentication.JwtBearer 2.0.0 已安装。

新的空 Core 2.0 项目(带有 JwtBearer 包)也没有 IServiceCollection 的扩展方法 AddJwtBearerAuthentication()。

老方法app.UseJwtBearerAuthentication()根本编译不通过:

Error   CS0619  'JwtBearerAppBuilderExtensions.UseJwtBearerAuthentication(IApplicationBuilder, JwtBearerOptions)' is obsolete: 'See https://go.microsoft.com/fwlink/?linkid=845470'

请帮忙

在 ConfigureServices 中使用以下代码配置 JWTBearer 身份验证:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(o =>
        {
            o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        }).AddJwtBearer(o =>
        {
            o.Authority = "https://localhost:54302";
            o.Audience = "your-api-id";
            o.RequireHttpsMetadata = false;
        });

        services.AddMvc();
    }

并在 Configure UseMvc() 之前添加 UseAuthentication():

app.UseAuthentication();

app.UseStaticFiles();

app.UseMvc();

有关详细示例,请参阅:https://github.com/aspnet/Security/blob/dev/samples/JwtBearerSample/Startup.cs#L51

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddJwtBearer(options => {
            options.Audience = "http://localhost:5001/";
            options.Authority = "http://localhost:5000/";
        });

https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x

Jwt认证配置方法:

// Configure authentication with JWT (Json Web Token).
public void ConfigureJwtAuthService(IServiceCollection services)
{
  // Enable the use of an [Authorize(AuthenticationSchemes = 
  // JwtBearerDefaults.AuthenticationScheme)]
  // attribute on methods and classes to protect.
  services.AddAuthentication().AddJwtBearer(cfg =>
  {
    cfg.RequireHttpsMetadata = false;
    cfg.SaveToken = true;
    cfg.TokenValidationParameters = new TokenValidationParameters()
    {
      IssuerSigningKey = JwtController.SecurityKey,
      ValidAudience = JwtController.Audience,
      ValidIssuer = JwtController.Issuer,
      // When receiving a token, check that we've signed it.
      ValidateIssuerSigningKey = true,
      // When receiving a token, check that it is still valid.
      ValidateLifetime = true,
      // This defines the maximum allowable clock skew when validating 
      // the lifetime. As we're creating the tokens locally and validating
      // them on the same machines which should have synchronised time,
      // this can be set to zero.
      ClockSkew = TimeSpan.FromMinutes(0)
    };
  });
}

现在在Startup.csConfigureServices()方法里面,可以调用ConfigureJwtAuthService( )方法配置Jwt认证。