Force MFA in Blazor using Azure AD and Continuous Access

This article shows how to force MFA from your application using Azure AD and a continuous access auth context. When producing software which can be deployed to multiple tenants, instead of hoping IT admins configure this correctly in their tenants, you can now force this from the application. Many tenants do not force MFA.


Blogs in this series

Steps to implement

  1. Create an authentication context in Azure for the tenant (using Microsoft Graph).
  2. Add a CA policy which uses the authentication context.
  3. Implement an authentication challenge using the claims challenge in the Blazor WASM.

Creating a conditional access authentication context

A continuous access (CA) authentication context was created using Microsoft Graph and a policy was created to use this. See the first blog in this series for details on setting this up.

Force MFA in the Blazor application

Now that the continuous access (CA) authentication context is setup and a policy is created requiring MFA, the application can check that the required acrs with the correct value is present in the id_token. We do this is two places, in the login of the account controller and in the OpenID Connect event sending the authorize request. The account controller Login method can be used to set the claims parameter with the required acrs value. By requesting this, the Azure AD policy auth context is forced.

public ActionResult Login(string? returnUrl, 
	string? claimsChallenge)
  // var claims = 
  //  "{\"access_token\":{\"acrs\":{\"essential\":true,\"value\":\"c1\"}}}";
  // var claims = 
  //  "{\"id_token\":{\"acrs\":{\"essential\":true,\"value\":\"c1\"}}}";
  var redirectUri = !string.IsNullOrEmpty(returnUrl) ? returnUrl : "/";

  var properties = new AuthenticationProperties { RedirectUri = redirectUri };

  if(claimsChallenge != null) 
	string jsonString = claimsChallenge.Replace("\\", "")
		.Trim(new char[1] { '"' });

	properties.Items["claims"] = jsonString;
	// lets force MFA using CAE for all sign in requests.
	  = "{\"id_token\":{\"acrs\":{\"essential\":true,\"value\":\"c1\"}}}";

  return Challenge(properties);

In the application an ASP.NET Core authorization policy can be implemented to force the MFA. All requests require a claim type acrs with the value c1, which we created in the Azure tenant using Microsoft Graph.

    .AddMicrosoftGraph("", scopes)

services.AddAuthorization(options =>
    options.AddPolicy("ca-mfa", policy =>
        policy.RequireClaim("acrs", AuthContextId.C1);

By using the account controller login method, only the login request forces the auth context. If the context needs to be forced everywhere, then a middleware using the OnRedirectToIdentityProvider event can be used to add the extra request parameter on every OIDC authorize request. The OnRedirectToIdentityProvider event can be used to add this to all requests, which has not already added the claims parameter. You could also only use this without the login implementation in the account controller.

services.AddRazorPages().AddMvcOptions(options =>
    var policy = new AuthorizationPolicyBuilder()
        .RequireClaim("acrs", AuthContextId.C1)
    options.Filters.Add(new AuthorizeFilter(policy));

services.Configure<MicrosoftIdentityOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>
    options.Events.OnRedirectToIdentityProvider = context =>

        return Task.FromResult(0);

Now all requests require the auth context which is used to require the CA MFA policy.



  1. […] Force MFA in Blazor using Azure AD and Continuous Access (Damien Bowden) […]

  2. […] Force MFA in Blazor using Azure AD and Continuous Access – Damien Bowden […]

  3. Douglas Fontans · · Reply

    Hi Damien,

    Please, could you accept my invite on LinkedIn? It’s would be amazing.
    So, do you have any article or tutorial about blazor and keycloak? I looking for, but don’t found.
    I’m from tax area, but I want to build a app, for this I have been studying blazor for last year.

    Yours contribution are excellent, congrats!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: