@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`
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.