【问题标题】:Missing extension method AddJwtBearerAuthentication() for IServiceCollection in .NET Core 2.0.NET Core 2.0 中 IServiceCollection 缺少扩展方法 AddJwtBearerAuthentication()
【发布时间】:2018-01-24 12:12:30
【问题描述】:

我已使用来自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 不记名身份验证:

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'

请帮忙。

【问题讨论】:

    标签: authentication jwt bearer-token .net-core-1.1 .net-core-2.0


    【解决方案1】:

    在 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

    【讨论】:

      【解决方案2】:

      配置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认证。

      【讨论】:

        【解决方案3】:
        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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多