Cancel button Azure B2C

Peter Tomodi 21 Reputation points
2022-02-03T11:45:03.293+00:00

If the user click the 'Cancel' button, Azure AD B2C will respond with an error message containing the error code -AADB2C90091.
How can I use Javascript to handle this error and redirect the handled error to the home page?

Microsoft Security | Microsoft Entra | Microsoft Entra External ID
0 comments No comments
{count} votes

Accepted answer
  1. JamesTran-MSFT 36,911 Reputation points Microsoft Employee Moderator
    2022-02-03T23:22:39.97+00:00

    @Peter Tomodi
    Thank you for your post!

    I wasn't able to find any JavaScript examples both through internal support requests or external forums, but I did find some related issues that might help point you in the right direction. To redirect a user back to the home page after selecting the "Cancel button", you'll either have to catch the error message or hide the Cancel button (via display:none in css file).

    When you call a sign-in policy, the redirect URI will be included in the URL. After you click the Cancel button, the user will be redirected to the URL you configured.

    `https://<b2ctenant>.b2clogin.com/<b2ctenant>.onmicrosoft.com/oauth2/v2.0/authorize?p=B2C_1_signupsignin  
    &client_id=11...  
    &nonce=defaultNonce  
    &redirect_uri=https://jwt.ms  
    &scope=openid  
    &response_type=id_token  
    &prompt=login`  
    

    171108-image.png

    Cancel button throws error on Azure AD B2C SignUp:
    The idea behind getting these errors is to catch them, and redirect the user where you want - for example sign-up or sign-in page, etc. The error is handled by the application based on the code sent back. For example, in an MVC app in the startup.auth.cs add the following to tell the application where to send the user. For more info.

     private Task AuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification)  
      
        {  
            notification.HandleResponse();  
            if (notification.ProtocolMessage.ErrorDescription != null && notification.ProtocolMessage.ErrorDescription.Contains("AADB2C90091"))  
            {  
                // If the user clicked the cancel button, redirect to default route  
                notification.Response.Redirect("/");  
            }  
            else if (notification.ProtocolMessage.ErrorDescription != null && notification.ProtocolMessage.ErrorDescription.Contains("AADB2C90118"))  
            {  
                // If the user clicked the reset password link, redirect to the reset password route   
                notification.Response.Redirect("/Home/ResetPassword");  
            }  
            else if (notification.Exception != null && notification.Exception.Message == "access_denied")  
            {  
                notification.Response.Redirect("/");  
            }  
            else  
            {  
                notification.Response.Redirect("/Home/Error?message=" + notification.ProtocolMessage.ErrorDescription);  
            }  
      
            return Task.FromResult(0);  
        }  
    

    Here's another example: active-directory-b2c-dotnet-webapp-and-webapi

      private Task OnAuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification)  
            {  
                notification.HandleResponse();  
      
                // Handle the error code that Azure AD B2C throws when trying to reset a password from the login page  
                // because password reset is not supported by a "sign-up or sign-in policy"  
                if (notification.ProtocolMessage.ErrorDescription != null && notification.ProtocolMessage.ErrorDescription.Contains("AADB2C90118"))  
                {  
                    // If the user clicked the reset password link, redirect to the reset password route  
                    notification.Response.Redirect("/Account/ResetPassword");  
                }  
                else if (notification.Exception.Message == "access_denied")  
                {  
                    notification.Response.Redirect("/");  
                }  
                else  
                {  
                    notification.Response.Redirect("/Home/Error?message=" + notification.Exception.Message);  
                }  
      
                return Task.FromResult(0);  
            }  
    

    Additional Links:
    When click 'Cancel' button password reset flow leads to "Error 401" - Our PG team worked on this issue.
    How to: Restart Azure AD B2C UserJourney when user cancels signup
    Sign up Sign in Error on Cancel

    If you have any other questions, please let me know.
    Thank you for your time and patience throughout this issue.

    ----------

    Please remember to "Accept Answer" if any answer/reply helped, so that others in the community facing similar issues can easily find the solution.


1 additional answer

Sort by: Most helpful
  1. Anonymous
    2023-10-31T15:24:02.5433333+00:00

    @Peter Tomodi
    I've been using @azure/msal-react module for my react app and this error happened whenever I clicked cancel button.
    However, I was able to get rid out of this. I am sharing my code snippet here.

    import { MsalAuthenticationTemplate } from '@azure/msal-react'
    import { InteractionType } from '@azure/msal-browser'
    
    <MsalAuthenticationTemplate
      interactionType={InteractionType.Redirect}
      authenticationRequest={{
        scopes: [
          'Your scope goes here'
          'https://milaaisignin.onmicrosoft.com/tasks-api/tasks.read'
        ]
      }}
      loadingComponent={Loader}
      errorComponent={(err) => {
        err.login()
        return <></>
      }}
    >
       Your content goes here.
    </MsalAuthenticationTemplate>
    

    I hope this helps. Thank you.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.