Blazor Azure AD B2C Custom URL

In this tutorial, you will learn how to override AccountController.cs in server-side Blazor so you can set a custom SignedOut URL when using Azure AD B2C authentication. If you have created a Blazor app with Azure AD B2C authentication, it was likely scaffolded using the helpful AzureADB2C.UI package. Unfortunately, since this is now included as a class library .dll, it takes a little effort to customize the routes.

By default, your auth-enabled Blazor web app will use routes such as AzureADB2C/Account/SignIn. Moreover, when a user logs out, they are taken to an unbranded page located at /AzureADB2C/Account/SignedOut. Both of these are hard-coded into the AzureADB2C.UI package’s AccountController.cs (see source code), but neither provide for a great user experience in a public-facing web app.

Disable existing Account Controller

To override the Account controller, you must first disable the existing AccountController. To do this, create a new class file in the root of your Blazor project and call it B2CExtensions.cs. Then, create a method to remove AzureADB2C.UI’s Account controller feature provider from the list of controller feature providers. The entire class might look like the following.

using Microsoft.Extensions.DependencyInjection;
using System.Linq;

namespace BlazorAzureADB2C
    public static class B2CExtensions
        public static IMvcBuilder RemoveADB2CAccountController(this IMvcBuilder builder)
            var ADB2CAccountController = builder.PartManager.FeatureProviders.FirstOrDefault(fp => fp.ToString()
            if (ADB2CAccountController != null)
            return builder;

The RemoveADB2CAccountController() method allows your app to avoid loading the default Account controller feature from the assembly. There is no need to also remove the compiled views provided by the Nuget package, because you can still use those with your custom controller.

Next, in your Blazor project’s Startup.cs file, invoke the method you just created. Within the ConfigureServices() block, you can simply chain it onto the AddRazorPages() call.


Depending on your project’s configuration, you could also chain the RemoveADB2CAccountController() call onto the AddMvc() or AddControllers() methods.

Customize AccountController.cs

Now it is time to create the new Account controller. Start by creating a Controllers folder in your Blazor project, and add a new AccountController.cs class. Feel free to use the source code for the existing controller as a guide.

I wanted my new controller to be able to use the options I set up when configuring the project to use my Azure AD B2C tenant, so I included the package’s namespace.

using Microsoft.AspNetCore.Authentication.AzureADB2C.UI;

Next, I did not like AzureADB2C included in the URL for my application’s user flows, such as sign-in and sign-out, so I changed the route. The previous code is commented out in the example below.

namespace BlazorAzureADB2C.Controllers
    public class AccountController : Controller

Now, the sign-in action will appear at Account/SignIn instead of AzureADB2C/Account/SignIn. Don’t forget to also update the links in Shared/LoginDisplay.razor.

Finally, I wanted users to be redirected to the home page upon successful sign-out instead of loading /AzureADB2C/Account/SignedOut. My new SignOut action looks like the following:

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);
    var callbackUrl = Url.Page("/", pageHandler: null, values: null, protocol: Request.Scheme);
    return SignOut(
        new AuthenticationProperties { RedirectUri = callbackUrl },

Add Controller to Blazor

The only thing left to do is to make sure your new controller is loaded into the project. Go back into Startup.cs and locate the Configure() method. Add controller action endpoints by calling the MapControllers() extension for the Endpoints middleware.

app.UseEndpoints(endpoints =>

The Bottom Line

Test your application by attempting to login and logout. For an app running on port 5000, you should be able to activate the SignIn userflow at http://localhost:5000/Account/SignIn. When you log out, you should be taken to your applications home page (or whatever location you configured in the controller). To test whether the previous controller was successfully disabled, make sure nothing is available at the old action locations, such as /AzureADB2C/Account/SignIn. That’s it! Feel free to make any other customizations to your AccountController that you see fit. Let me know in the comments if this tutorial helped you.

Was this article helpful? Share it with your friends:

Share on email
Share on facebook
Share on twitter
Share on reddit

10 thoughts on “Blazor Azure AD B2C Custom URL”

  1. Hi Bradley,
    This is very helpful. The only article I was able to find which spells out how to prevent redirection into the “black hole” of /Account/SignedOut.

    Another problem I am struggling with is being able to
    A) upon sign-in (or profile edit) to return to the same route from which this action was invoked; and
    B) Supply a “state” payload with sign-in/profile-edit request. E.g., some sort of correlation Id which would help me with application state management.

  2. Thank you very much for this article. This was exactly what I was looking for (albeit it took a while to find what I needed).

  3. Hello Again!

    Also, I’m having some issues.
    I’m trying to update the Sign In method, but when I follow your steps, the application redirect to Azure automatically, using the Sign In method that I created in the Account controller. I don’t even have to click to Sign In to do that.
    How can I prevent that to happen?

    Thank you

  4. Hi Bradley, Thank you for sharing your knowledge. I am looking into using Azure B2C for protecting aspnet core web api and pass over the sign in user token to the web api and utilize roles and policy. Can you point me into the right direction. Thanks

  5. Thanks, it’s really helpful. MS just did some changes and the FeatureProvider name has changed to Microsoft.Identity.Web.UI.MicrosoftIdentityAccountControllerFeatureProvider


Leave a Comment

Looking for more?

You have visited our site before, and we appreciate you!

If you found the tutorials helpful, enter your email address for more free C# tips and tricks.

Can't get enough C#?

Enter your email address for more free C# tutorials and tips.