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

IDi*_*zor 6 authentication jwt bearer-token .net-core-1.1 .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并使用了metapackage Microsoft.AspNetCore.All).我已将所有可能的nuget包更新到最新版本.

在.NET Core 1.1中,我以这种方式添加JWT承载认证:

app.UseJwtBearerAuthentication(); // from Startup.Configure()
Run Code Online (Sandbox Code Playgroud)

根据http://www.talkingdotnet.com/whats-new-in-asp-net-core-2-0/对于Core 2.0,新方法是致电:

services.AddJwtBearerAuthentication(); // from Startup.ConfigureServices()
Run Code Online (Sandbox Code Playgroud)

但是方法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'
Run Code Online (Sandbox Code Playgroud)

请帮忙.

Sil*_*hus 9

在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();
    }
Run Code Online (Sandbox Code Playgroud)

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

app.UseAuthentication();

app.UseStaticFiles();

app.UseMvc();
Run Code Online (Sandbox Code Playgroud)

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