【问题标题】:AzureAd JwtBearer No authenticationScheme was specified, and there was no DefaultChallengeScheme foundAzureAd JwtBearer 没有指定 authenticationScheme,也没有找到 DefaultChallengeScheme
【发布时间】:2021-10-18 15:41:33
【问题描述】:

我正在尝试修改我的 AzureAd 身份验证以使用 SignalR。所以我更改了身份验证,以便添加更多选项。

在我使用 services.AddMicrosoftIdentityWebApiAuthentication(Configuration, "AzureAd"); 之前,它运行良好。

据此https://docs.microsoft.com/en-us/azure/active-directory/develop/scenario-protected-web-api-app-configuration#:~:text=you%20can%20also%20write%20the%20following%20(which%20is%20equivalent) 我应该可以使用services.AddAuthentication(AzureADDefaults.JwtBearerAuthenticationScheme) .AddMicrosoftIdentityWebApi(Configuration, "AzureAd");

但是,当我点击我的 API 时,我得到了 System.InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action<AuthenticationOptions> configureOptions).

【问题讨论】:

标签: c# asp.net authentication azure-active-directory


【解决方案1】:

错误:System.InvalidOperationException:没有 authenticationScheme 指定,并且没有找到 DefaultChallengeScheme。默认 可以使用 AddAuthentication(string defaultScheme) 或 AddAuthentication(Action 配置选项)。

使用 Microsoft 标识平台(refer) 登录用户时, 添加 Microsoft.Identity.Web 和 Microsoft.Identity.Web.UI NuGet 包

根据错误尝试通过检查以下方式添加默认方案:

在配置服务方法下的启动类中

using Microsoft.Identity.Web;

public void ConfigureServices(IServiceCollection services)
{

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddMicrosoftIdentityWebApi(
           options => {  Configuration.Bind("AzureAd", options);

                //options.Events = new JwtBearerEvents
             
                  //

                   //custom code
}

 public void ConfigureServices(IServiceCollection services)
{
   
services .AddAuthentication(JwtBearerDefaults.AuthenticationScheme) 

         .AddMicrosoftIdentityWebApi(Configuration.GetSection("AzureAd"));

services.Configure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, 

    options => {
 
      options.Events.OnAuthenticationFailed = async failedContext => {

 if (failedContext.Exception.HResult == -2147024891) 
{ 
    failedContext.Response.StatusCode = 403; 

    await failedContext.Response.CompleteAsync(); 

  } 
           };

              });
//
//
}

(或)

public void ConfigureServices(IServiceCollection services)
        {
               //
         services.AddAuthentication(sharedOptions =>
          {
         sharedOptions.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
    
         
        sharedOptions.DefaultChallengeScheme=JwtBearerDefaults.AuthenticationScheme;
    
        })
            .AddMicrosoftIdentityWebApi(
               options =>
               {
               //
                Configuration.Bind("AzureAd", options);
                    //options.Events = new JwtBearerEvents
                   
                   //
                   
     
               //custom code
          }
    
         }

还要确保你有这行 cod app.UseAuthentication();在 app.UseAuthorization() 顶部的 Configure() 方法中(如果存在)。

同时检查this

参考资料:

  1. Azure-Samples
  2. microsoft-identity-web
  3. SO ref-signalR

【讨论】:

    猜你喜欢
    • 2019-11-02
    • 1970-01-01
    • 2018-02-26
    • 2019-03-22
    • 1970-01-01
    • 2019-08-02
    • 2021-05-24
    • 1970-01-01
    • 2021-10-11
    相关资源
    最近更新 更多