Search code examples
azure-ad-b2cazure-application-insightsazure-ad-b2c-custom-policy

Global exception page is not being rendered for ADB2C for exceptions instead the login page is rendered. Find a way to render custom error page


I am using azureadb2c integration. On the customised login page which is using a third party login just like google or fb after successfully entering user and password we have a consent page. When user accept the consent it redirects to our Home page but when the user clicks on not give consent by default it is taking the user to login page. What I want to do is instead of taking the user to the login page I want it to be redirected to a custom error page. I have gone through various answers here for handling this using an additional orchestration step but none worked.

Last I found that api.error should handle all unhandled exception but I dont know why it is being ignored and the login screen is shown.

This is the exception that I found in Application Insight:

"Exception": {
        "Kind": "Handled",
        "HResult": "80131500",
        "Message": "An invalid response was received : 'Error: access_denied,Error Description: ConsentNotGiven'",
        "Data": {
          "IsPolicySpecificError": false
        }

This is my api.error Content Definition, I tried replacing the LoadUri and tested it in Network in Google Debug Console but I cant see even that particular url getting hit.

<ContentDefinition Id="api.error">
        <LoadUri>~/tenant/templates/AzureBlue/exception.cshtml</LoadUri>
        <RecoveryUri>~/common/default_page_error.html</RecoveryUri>
        <DataUri>urn:com:microsoft:aad:b2c:elements:contract:globalexception:1.2.1</DataUri>
        <Metadata>
          <Item Key="DisplayName">Error page</Item>
        </Metadata>
      </ContentDefinition>

It will be helpful if someone can show how I can get this page rendered on every exception. Thanks


Solution

  • I tried multiple solution to get the api.error page working but I wasn't able to accomplish the task. My major aim was to render the custom error page whenever user clicks the cancel or not giving accent button which was throwing an exception with above code : AADB2C90273

    Finally I have got the solution and it was to edit the Startup file and to fetch the event that gets triggered when the exception is raised in ADB2C.

    The project being in .Net Core 3.1 it was quite difficult to get the way to set up OpenIdConnectOptions with AzureADB2c events as we weren't able to find any document related to it.

    I added below code in the Startup.cs file in the ConfigureServices method :

    services.Configure<OpenIdConnectOptions>(Configuration.GetSection("AzureAdB2C"))
                    .Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>
                    {
                        options.Events.OnMessageReceived = (context) =>
                        {
                            if (!string.IsNullOrEmpty(context.ProtocolMessage.Error) &&
                                !string.IsNullOrEmpty(context.ProtocolMessage.ErrorDescription) &&
                                context.ProtocolMessage.ErrorDescription.StartsWith("AADB2C90273"))
                            {
                                context.Response.Redirect("/Home/Error");
                                context.HandleResponse();
                            }
                            return Task.CompletedTask;
                        };
                    });
    

    where Configuration.GetSection("AzureAdB2C") is used to get the properties defined in json file and OnMessageReceived was the event that was getting triggered when adb2c was having an exception. I redirected it to the custom error page that I created.

    I hope this helps someone.