簡體   English   中英

asp.net core 2.2 web api “No authenticationScheme was specified, and there was no DefaultChallengeScheme found” error Azure AD

[英]asp.net core 2.2 web api “No authenticationScheme was specified, and there was no DefaultChallengeScheme found” error Azure AD

配置看起來不錯。 但是在嘗試添加基於 Azure AD 的身份驗證時出現錯誤。

Startup.cs => ConfigureServices

public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthentication(options =>
    {
        options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    })
    .AddAzureADBearer(options => Configuration.Bind("AzureAd", options));

    // Rest of the code
}

Startup.cs => Configure

public void Configure(IApplicationBuilder app, IMicrosoftServersHealthCheckService microsoftServersHealthCheckService)
{
    _logger.LogTrace($"Inside Configure for Environment: {Environment.EnvironmentName}");
            
    app.UseMiddleware<APIResponseMiddleware>();
    app.UseExceptionHandler(errorApp =>
    {
        errorApp.Run(async context =>
        {
            var errorFeature = context.Features.Get<IExceptionHandlerFeature>();
            var exception = errorFeature.Error;
            _logger.LogError(exception, exception.Message);
            ExceptionHandlerFilter.HandleExceptionAsync(context, exception).Wait();
        });
    });
    
    if (Environment.IsDevelopment())
    {
        // The following will be picked up by Application Insights.
        _logger.LogInformation("Configuring for Development environment");
        //app.UseDeveloperExceptionPage();                
    }
    else
    {
        // The following will be picked up by Application Insights.
        _logger.LogInformation($"Configuring for {Environment.EnvironmentName} environment");
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        //app.UseHsts();
    }

    app.UseSwagger();

    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "DNS API");

    });

    app.UseAuthentication();
    app.UseMvc();
    
}

Message: "No authenticationScheme was specified, and there was no DefaultChallengeScheme found."

Stack Trace: System.InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found.\r\n at Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, String scheme, AuthenticationProperties properties)\r\n at Microsoft.AspNetCore.Mvc.ChallengeResult.ExecuteResultAsync(ActionContext context)\r\n at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult result)\r\n at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsync[TFilter,TFilterAsync]()\r\n at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context)\r\n at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAlwaysRunResultFilters()\r\n at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()\r\n at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()\r\n at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)\r\n at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)\r\n at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)\r\n at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)\r\n at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)\r\n at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)\r\n at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context)

如果您想使用 Azure AD 投影您的 web API,請參考以下代碼

  1. 安裝 package
Install-Package Microsoft.Identity.Web -Version 1.6.0
  1. 代碼
public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddMicrosoftIdentityWebApi(Configuration, "AzureAd");
    }

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    // more code
    app.UseAuthentication();
    app.UseAuthorization();
    // more code
}

有關更多詳細信息,請參閱此處此處

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM