使用 Microsoft.AspNetCore.Authentication.AzureADB2C.UI 时如何覆盖/替换错误页面处理?

问题描述 投票:0回答:2

我在 ASP.Net Core 2.2 MVC Web 应用程序中使用 Azure AD B2C .Net core Microsoft.AspNetCore.Authentication.AzureADB2C.UI 库(使用 NuGet 安装)。

我希望能够更改错误页面,但它会忽略任何自定义或开发人员模式错误页面。

有谁知道我如何覆盖错误处理和/或该库的任何其他页面?

这是针对任何 Azure B2C 错误返回的页面 (github)。 https://github.com/aspnet/AspNetCore/blob/master/src/Azure/AzureAD/Authentication.AzureADB2C.UI/src/Areas/AzureADB2C/Pages/Account/Error.cshtml

我创建了一个自定义错误页面,并在启动中包含以下内容。其他所有内容都使用此自定义页面或默认开发人员异常页面,具体取决于模式。

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
else
{
    app.UseExceptionHandler("/Account/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}
c# azure asp.net-core azure-active-directory azure-ad-b2c
2个回答
3
投票

将其放入 Startup.Configure() 方法中:

app.UseRewriter(new RewriteOptions().Add(context =>
{
    if (context.HttpContext.Request.Path == "/AzureADB2C/Account/SignedOut")
    {
        context.HttpContext.Response.Redirect("/Home/SignedOut");
    }
}));

重定向到您想要的任何页面。


0
投票

错误路径的默认值为

new PathString("/MicrosoftIdentity/Account/Error")

您可以使用新的路径值覆盖此路径值,您可以为 adb2c 错误路径设置任何自定义错误页面。有 mergeOptions 可用于更新此值。要更新路径,请按照以下步骤操作

使用 adb2c 进行配置时,添加 Error Path 属性并在 appsettings.json

中设置路径值,如下所示
 "AzureAdB2C": {
   "Instance": "https://test.b2clogin.com",
   "ClientId": "clientId",
   "ClientSecret": "ClientSecret",
   "Domain": "test.onmicrosoft.com",
   "SignedOutCallbackPath": "/signout/SUSI_1",
   "SignUpSignInPolicyId": "SUSI_1",
   "ErrorPath": "/Home/Error"
},

Program.cs中传递此配置

builder.Services.AddMicrosoftIdentityWebAppAuthentication(builder.Configuration, Constants.AzureAdB2C)
    .EnableTokenAcquisitionToCallDownstreamApi()
    .AddMicrosoftGraph(builder.Configuration.GetSection("GraphApi"))
    .AddInMemoryTokenCaches();
builder.Services.Configure<OpenIdConnectOptions>(builder.Configuration.GetSection("AzureAdB2C"));

注意:这里你会得到设置Error Path的属性,但是这个属性是私有设置的,所以你不能设置。

© www.soinside.com 2019 - 2024. All rights reserved.