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

  1. 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).
  2. 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.
  3. 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 custom ErrorModel (Error.cshtml.cs) to explicitly log a Serilog Error event when this page renders. This ensures identity failures are written to logs/errors-.txt and sent via Serilog email (team.crystal@outlook.com).

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> and ILoginErrorAccessor.
  • Log _logger.LogError(...) on OnGet() 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.csproj to confirm Razor Compilation and C# code build without errors.

Manual Verification

  • Verify navigating to /MicrosoftIdentity/Account/Error renders the new layout and logs a Serilog warning/error entry.