Change Azure AD B2C SignOut URL (Change AzureADB2C/Account/SignedOut to custom URL)

If you look at the source code of the AccountController in the Microsoft.AspNetCore.Authentication.AzureADB2C.UI nuget package, you can see that the callbackUrl is hard-coded to (/AzureADB2C)/Account/SignedOut.

But there is no requirement to use that controller. Just call your own SignOut action on your own controller. Copy-paste the code from the AzureADB2C SignOut action and change the callbackUrl to your own.

Edit _LoginPartial.cshtml: remove asp-area="AzureADB2C" and use your own for asp-controller and asp-action.


[Additional information to the answer provided by @Marcel W and to the question asked by @Sven]

A bit late to the party but in case it helps others :

  • Blazor server app .net core 3.1
  • Authentication : Azure B2C

Original code is in the following repository

You'll see that in signout method the callback url is unfortunately hard coded.

        [HttpGet("{scheme?}")]
        public async Task<IActionResult> SignOut([FromRoute] string scheme)
        {
            scheme = scheme ?? AzureADB2CDefaults.AuthenticationScheme;
            var authenticated = await HttpContext.AuthenticateAsync(scheme);
            if (!authenticated.Succeeded)
            {
                return Challenge(scheme);
            }

            var options = _options.Get(scheme);

            var callbackUrl = Url.Page("/Account/SignedOut", pageHandler: null, values: null, protocol: Request.Scheme);
            return SignOut(
                new AuthenticationProperties { RedirectUri = callbackUrl },
                options.AllSchemes);
        }

So the idea is to take the code and create the same behavior in the project.

  1. Create Areas folder
  2. Create AzureADB2C folder inside Areas folder
  3. Create Pages folder inside AzureADB2C folder
  4. Create Account folder inside Pages folder
  5. Create SignOut.cshtml file inside Account folder
  6. Copy/Paste following code

@page
@using Microsoft.AspNetCore.Authentication
@using Microsoft.AspNetCore.Authentication.AzureADB2C.UI
@using Microsoft.Extensions.Options
@inject IOptionsMonitor<AzureADB2COptions> Options
@attribute [IgnoreAntiforgeryToken]
@functions {
    public async Task<IActionResult> OnPost([FromRoute] string scheme)
    {
        scheme = scheme ?? AzureADB2CDefaults.AuthenticationScheme;
        var authenticated = await HttpContext.AuthenticateAsync(scheme);
        if (!authenticated.Succeeded)
        {
            return Challenge(scheme);
        }

        var options = Options.Get(scheme);

        var callbackUrl = Url.Page("/", pageHandler: null, values: null, protocol: Request.Scheme);
        return SignOut(
            new AuthenticationProperties { RedirectUri = callbackUrl },
            options.AllSchemes);
    }
}

The final modification will take place in LoginDisplay.razor file. We need to create a form that'll do our "post" in order to sign off the user

Replace the following line in this file

    <a href="AzureADB2C/Account/SignOut">Log out</a>

by

    <form method="post" action="AzureADB2C/Account/SignOut">
        <button type="submit" class="nav-link btn btn-link">Log out</button>
    </form>

Below a screenshot that illustrates the directory structure

enter image description here