【问题标题】:ASP Core 2.0 app.UseJwtBearerAuthentication failsASP Core 2.0 app.UseJwtBearerAuthentication 失败
【发布时间】:2017-12-24 14:09:14
【问题描述】:

我使用 dot net core 1.1 开发了一个 Web 应用程序。在我更新到 asp core 2.0 之前它工作正常。然后在尝试使用该应用程序时,它会报告此错误:

InvalidOperationException:未指定 authenticationScheme,并且 没有找到 DefaultChallengeScheme。

JwtBearerAuthentication 的身份验证配置,在Startup.csConfigure 方法中之前如下所示:

app.UseJwtBearerAuthentication(new JwtBearerOptions
            {
                AutomaticAuthenticate = true,
                AutomaticChallenge = true,
                TokenValidationParameters = tokenValidationParameters
            });

在遇到此错误后,我已将其更新为这个:

app.UseJwtBearerAuthentication(new JwtBearerOptions
        {
            AutomaticAuthenticate = true,
            AutomaticChallenge = true,
            TokenValidationParameters = tokenValidationParameters,
            AuthenticationScheme = JwtBearerDefaults.AuthenticationScheme
        });

但错误消息仍然存在。我是在寻找错误的地方,还是必须添加其他配置才能使身份验证系统正常工作?

【问题讨论】:

  • 身份验证在 2.0 中已更改。更多信息:github.com/aspnet/Announcements/issues/262
  • TL;DR:只需要中间件是app.UseAuthentication();,并且您将身份验证处理程序定义为ConfigureServices() 中的服务。

标签: asp.net-core restful-authentication


【解决方案1】:

我刚刚关注了这篇文章,一切都很完美。

Migrating Authentication and Identity to ASP.NET Core 2.0

希望它可以帮助其他人。

TG。

【讨论】:

  • 正是我想要的。
【解决方案2】:

您可以使用此代码代替您自己的代码来解决您的问题:

    services.AddAuthentication(options =>
    {
     options.DefaultAuthenticateScheme = 
     JwtBearerDefaults.AuthenticationScheme;
     options.DefaultChallengeScheme = 
     JwtBearerDefaults.AuthenticationScheme;
     options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
    })

【讨论】:

    猜你喜欢
    • 2018-02-12
    • 1970-01-01
    • 2018-07-25
    • 2021-07-31
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多