Authentication.AzureADB2C.UI - 如何自定义错误页面 - .Net Core 3.1
Authentication.AzureADB2C.UI - How to customize Error Page - .Net Core 3.1
我有一个项目,我们正在使用包 Microsoft.AspNetCore.Authentication.AzureADB2C.UI 通过 Azure AD B2C 进行身份验证。
有时,如果会话过期或用户尝试直接从 Azure AD B2C 登录页面登录,将出现此错误页面 [错误页面](https://github.com/dotnet/aspnetcore/blob/master/src/Azure/AzureAD/Authentication.AzureADB2C.UI/src/Areas/AzureADB2C/Pages/Account/Error.cshtml):
但是,我想自定义此页面,但我不知道该怎么做。
我已经通过替换注销方法自定义 AzureADB2C Controller 以使用自定义注销页面。但是,此控制器中没有 "Error" 方法。
有人可以告诉我去的方向吗?
谢谢
更新
除了提供的修复之外,我还修改了下面的代码以强制用户在发生远程故障时再次重定向到登录页面。我注意到这解决了大多数人会收到该错误的问题:
`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();
}
我有一个项目,我们正在使用包 Microsoft.AspNetCore.Authentication.AzureADB2C.UI 通过 Azure AD B2C 进行身份验证。
有时,如果会话过期或用户尝试直接从 Azure AD B2C 登录页面登录,将出现此错误页面 [错误页面](https://github.com/dotnet/aspnetcore/blob/master/src/Azure/AzureAD/Authentication.AzureADB2C.UI/src/Areas/AzureADB2C/Pages/Account/Error.cshtml):
但是,我想自定义此页面,但我不知道该怎么做。
我已经通过替换注销方法自定义 AzureADB2C Controller 以使用自定义注销页面。但是,此控制器中没有 "Error" 方法。
有人可以告诉我去的方向吗?
谢谢
更新
除了提供的修复之外,我还修改了下面的代码以强制用户在发生远程故障时再次重定向到登录页面。我注意到这解决了大多数人会收到该错误的问题:
`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();
}