【问题标题】:asp.net core 2.2 web api "No authenticationScheme was specified, and there was no DefaultChallengeScheme found" error Azure ADasp.net core 2.2 web api“没有指定 authenticationScheme,并且没有找到 DefaultChallengeScheme”错误 Azure AD
【发布时间】:2021-05-24 21:17:07
【问题描述】:

配置看起来不错。但我在尝试添加基于 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&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; 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)

【问题讨论】:

    标签: asp.net-core azure-active-directory asp.net-core-webapi asp.net-core-2.2


    【解决方案1】:

    如果您想使用 Azure AD 来投影您的 Web API,请参考以下代码

    1. 安装包
    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
    }
    

    更多详情请参考herehere

    【讨论】:

    • 我的应用程序正在使用 2.2 版的 asp.net 核心版本,这个包 Microsoft.Identity.Web 是否可以与 2.2 的 api 应用程序一起使用?您共享的第一个链接中的一个先决条件指定 .NET Core SDK 3.1。这个要求是否意味着我需要在 IIS 服务器上安装 SDK,还是意味着它可以在使用 .net core 3.1 或更高版本构建的应用程序上运行?请澄清!
    • @blogs4t 包Microsoft.Identity.Web 需要net ore 3.1 或更高版本:nuget.org/packages/Microsoft.Identity.Web/1.6.0。如果你使用net core 2.2,请参考blogs.aaddevsup.xyz/2019/12/…
    猜你喜欢
    • 2019-03-22
    • 2019-11-02
    • 2019-08-02
    • 1970-01-01
    • 2018-02-26
    • 2021-10-18
    • 1970-01-01
    • 2019-06-23
    • 2018-04-29
    相关资源
    最近更新 更多