首页 > 解决方案 > Authentication.AzureADB2C.UI - 如何自定义错误页面 - .Net Core 3.1

问题描述

我有一个项目,我们正在使用包 Microsoft.AspNetCore.Authentication.AzureADB2C.UI 对 Azure AD B2C 进行身份验证。有时,如果会话过期或用户尝试直接从 Azure AD B2C 登录页面登录,则会出现此错误页面 [Error Page](https://github.com/dotnet/aspnetcore/blob/master/src /Azure/AzureAD/Authentication.AzureADB2C.UI/src/Areas/AzureADB2C/Pages/Account/Error.cshtml): 错误页面

但是,我想自定义此页面,但我不知道该怎么做。

我已经通过替换 Sign Out 方法自定义AzureADB2C 控制器以使用自定义的注销页面。但是,此控制器中没有“错误”方法。

有人可以告诉我一个方向吗?

谢谢

更新

除了提供的修复之外,我还修改了下面的代码,以在发生远程故障时强制用户再次重定向到登录页面。我注意到这解决了大多数时候有人会收到该错误:

`public class AzureADB2COpenIdConnectOptionsConfigurator : IConfigureNamedOptions<OpenIdConnectOptions>`

(...)

public void Configure(string name, OpenIdConnectOptions options)
        {
           (...)
            options.Events.OnRemoteFailure = WrapOpenIdConnectEvent(options.Events.OnRemoteFailure, OnRemoteFailture);
           (...)
}

  private Task OnRemoteFailture(RemoteFailureContext context)
        {
            // Log exception
            _logger.LogInformation("Azure - Failure Sign In - ContextFailure: " + context.Failure.ToString());

            // Redirect user to SignIn, most of the times, the user will be simply logged in and won't see the developer page exception anymore
            context.Response.Redirect("/AzureADB2C/Account/SignIn");

            context.HandleResponse();

            return Task.CompletedTask;
        }

标签: c#asp.net-coreazure-ad-b2cmsalazure-authentication

解决方案


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.

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

推荐阅读