Override Microsoft Identity Auth Error Page to Fix Misleading Production Message
Override Microsoft Identity Auth Error Page to Fix Misleading Production Message
Replace the default misleading “Development Mode” error page generated by Microsoft.Identity.Web.UI with a clean, branded, user-friendly authentication error page in Production, while ensuring Serilog logs and emails identity errors.
Clarifications & Architecture
- Scope of
/MicrosoftIdentity/Account/Error:- This page is only navigated to during Microsoft Identity / Azure AD B2C authentication flows (e.g., OIDC callback errors, session timeouts, login cancellation).
- Impact on Standard Application Errors:
- All standard application errors (database exceptions, unhandled runtime errors, etc.) remain completely unaffected. They continue to be caught by
app.UseExceptionHandler("/Error")(Pages/Error.cshtml) and logged/emailed by Serilog automatically.
- All standard application errors (database exceptions, unhandled runtime errors, etc.) remain completely unaffected. They continue to be caught by
- Serilog Integration for Identity Errors:
- Because Microsoft Identity UI catches OIDC errors gracefully and redirects to
/MicrosoftIdentity/Account/Error, ASP.NET Core doesn’t treat them as unhandled 500 crashes. - We will inject
ILogger<ErrorModel>into our customErrorModel(Error.cshtml.cs) to explicitly log a SerilogErrorevent when this page renders. This ensures identity failures are written tologs/errors-.txtand sent via Serilog email (team.crystal@outlook.com).
- Because Microsoft Identity UI catches OIDC errors gracefully and redirects to
User Review Required
Important
This change overrides Areas/MicrosoftIdentity/Pages/Account/Error.cshtml and Error.cshtml.cs locally in ErpCrystal_MFG.Web.
In Production, end-users encountering an authentication failure will see a clean error card with the Request ID and a button back to the home page instead of the misleading “Development Mode” notice.
Proposed Changes
Web Project (ErpCrystal_MFG.Web)
[NEW] Error.cshtml
- Local Razor Page shadowing
Microsoft.Identity.Web.UI’s embedded error page. - Render Request ID if available.
- Display a clean generic card in Production (
<environment exclude="Development">) with instructions to try logging in again or file a support request.
[NEW] Error.cshtml.cs
- PageModel for
Areas/MicrosoftIdentity/Pages/Account/Error.cshtml. - Inject
ILogger<ErrorModel>andILoginErrorAccessor. - Log
_logger.LogError(...)onOnGet()so Serilog records the identity error and sends an email alert.
Verification Plan
Automated Build Verification
- Run
dotnet build ErpCrystal_MFG.Web/ErpCrystal_MFG.Web.csprojto confirm Razor Compilation and C# code build without errors.
Manual Verification
- Verify navigating to
/MicrosoftIdentity/Account/Errorrenders the new layout and logs a Serilog warning/error entry.