【问题标题】:ASP.NET Core 3.1 how to return 401 Unauthorized instead of Challenge with Azure AD B2CASP.NET Core 3.1 如何使用 Azure AD B2C 返回 401 Unauthorized 而不是 Challenge
【发布时间】:2020-03-20 21:05:59
【问题描述】:

在我的 API 控制器中,我希望 [Authorize] 属性返回 401 Unauthorized 而不是强制 Azure AD B2C 登录质询并将该响应返回到我的前端(对 API 进行 ajax 调用)。

由于 Azure CORS 策略,质询重定向不起作用。我找到了多个答案,但似乎都不起作用(没有返回 401,它仍然尝试重定向到 Azure B2C 挑战)。

API 控制器:

[Route("api/[controller]")]
[ApiController]
[Authorize]
public class HierarchyController : ControllerBase
// ...

这是我当前的Startup.ConfigureServices 代码:

// ...
services.Configure<AzureADB2COptions>(opt => 
            Configuration.GetSection("AzureAdB2C").Bind(opt));

services.AddAuthentication(AzureADB2CDefaults.AuthenticationScheme)
            .AddAzureADB2C(opt => Configuration.Bind("AzureAdB2C", opt))
            .AddCookie(opt =>
            {
                opt.Events = new CookieAuthenticationEvents
                {
                    OnRedirectToLogin = ctx =>
                    {
                        ctx.Response.StatusCode = 401;
                        return Task.CompletedTask;
                    }
                };
            });

我也尝试过使用:

services.ConfigureApplicationCookie(options =>
        {
            options.Events.OnRedirectToLogin = context =>
            {
                context.Response.Headers["Location"] = context.RedirectUri;
                context.Response.StatusCode = 401;
                return Task.CompletedTask;
            };
        });

在这两种情况下,将OnRedirectToLogin 切换为OnRedirectToAccessDenied。这些都不起作用。 这些事件中的断点永远不会被命中,并且在执行未经授权的 API 调用后,我收到此错误:

Access to XMLHttpRequest at &lt;MY_B2C_ADDR&gt; (redirected from 'https://localhost:44394/api/Hierarchy') from origin 'https://localhost:44394' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

编辑: ConfigureServices

// ...
app.UseRouting(); 

app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
      endpoints.MapControllerRoute(
           name: "default",
           pattern: "{controller}/{action=Index}/{id?}");
});

【问题讨论】:

    标签: c# api asp.net-core asp.net-web-api azure-ad-b2c


    【解决方案1】:

    对于将来阅读的任何人(或我) - 我已经解决了这个问题。 在ConfigureServices 做:

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = AzureADB2CDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultSignOutScheme = AzureADB2CDefaults.AuthenticationScheme;
            })
                .AddAzureADB2C(options => Configuration.Bind("AzureAdB2C", options))
                .AddCookie(options =>
                {
                    options.Events = new CookieAuthenticationEvents
                    {
                        OnRedirectToLogin = context =>
                        {
                            if (context.Request.Path.StartsWithSegments("/api"))
                            {
                                context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                            }
                            else
                            {
                                context.Response.Redirect(context.RedirectUri);
                            }
    
                            return Task.FromResult(0);
                        }
                    };
                });
    

    【讨论】:

    • 我有完全相同的问题,但在 .NET Core 3.1 中,我不知道如何通过调用 AddMicrosoftIdentityWebAppAuthentication 方法来做到这一点,这似乎是添加 ADB2C 的推荐方法到应用程序(基于示例)。不过我可能是错的。有没有人设法使用 .NET Core 3.1 实现同样的目标?
    猜你喜欢
    • 2020-01-01
    • 1970-01-01
    • 2016-04-05
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多