c# - 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;
}
解决方案
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();
}
推荐阅读
- schema.org - 结构化数据显示“属性 publisher.itemtype 的值无效。”
- java - ArrayIndexOutOfBoundException,同时在JAVA中从数组中分离奇数
- reactjs - 在 react-redux 中调度 2 个同步操作时,状态不会在第一个操作时更新
- typescript - 对于可用于浏览器或节点的 Typescript 模块,我应该使用什么模块构建策略?
- javascript - 如何从多选框中控制多选以显示值
- php - AJAX 和 JSON 代码无法从数据库中获取数据
- c++ - C++ 中未初始化的成员布尔变量的默认值是多少?
- css - 使用css来回旋转图标
- database - 添加日期过滤器 Pentaho 报告
- android - 在 AOSP 中对系统服务进行更改后构建哪个目标?