title: Azure b2c authorization return 401 (Unauthorized) in Blazor WASM
body: <p>I'm using Azure AD B2C as the authentication provider for ASP.NET Blazor WASM application and <code>API</code>. So the problem is that I'm not able to access the <strong>API</strong> endpoints from the <code>client</code> which is Blazor WASM. When I make the request to the API I get 401 (Unauthorized) response.</p>
  <p>Here is the console error</p>
  <pre><code>crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
        Unhandled exception rendering component: Response status code does not indicate success: 401 (Unauthorized).
  System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized).
     at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
     at System.Net.Http.Json.HttpClientJsonExtensions.&lt;GetFromJsonAsyncCore&gt;d__13`1[[Organizer.Web.Shared.Models.SuccessResultModel`1[[Organizer.Web.Shared.DTOs.UserDto, Organizer.Web.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Organizer.Web.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()
     at Organizer.Web.Pages.Authentication.AccountProfile.OnInitializedAsync() in A:\Software_Development\2022\Organizer\Organizer.Web.Pages\Authentication\AccountProfile.razor.cs:line 57
     at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
     at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
  </code></pre>
  <p>B2C Client settings
  <a href="https://i.stack.imgur.com/oSY8q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oSY8q.png" alt="Azure Client Settings" /></a></p>
  <p>B2C API settings
  <a href="https://i.stack.imgur.com/aIKVr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aIKVr.png" alt="enter image description here" /></a></p>
  <p>Blazor Client Appsettings and Program.cs</p>
  <pre class="lang-cs prettyprint-override"><code>builder.Services.AddHttpClient(&quot;Organizer.API&quot;, client =&gt; client.BaseAddress = new Uri(&quot;https://localhost:7149&quot;))
      .AddHttpMessageHandler&lt;BaseAddressAuthorizationMessageHandler&gt;();
  
  builder.Services.AddScoped(sp =&gt; sp.GetRequiredService&lt;IHttpClientFactory&gt;().CreateClient(&quot;Organizer.API&quot;));
  
  builder.Services.AddMsalAuthentication(options =&gt;
  {
      builder.Configuration.Bind(&quot;AzureAdB2C&quot;, options.ProviderOptions.Authentication);
      options.ProviderOptions.DefaultAccessTokenScopes.Add(
          &quot;https://mydomain.onmicrosoft.com/60dbe9eb-056c-400d-a98d-c5c95b2bb000/Data.Read&quot;);
      options.ProviderOptions.LoginMode = &quot;redirect&quot;;
  });
  </code></pre>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;AzureAdB2C&quot;: {
      &quot;Authority&quot;: &quot;https://mydomain.b2clogin.com/mydomain.onmicrosoft.com/B2C_1_susi&quot;,
      &quot;ClientId&quot;: &quot;f2161189-4bc6-4c26-99ae-a82b6729ab33&quot;,
      &quot;ValidateAuthority&quot;: false
    }
  }
  </code></pre>
  <p>API</p>
  <pre class="lang-cs prettyprint-override"><code>builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
      .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection(&quot;AzureAdB2C&quot;));
  </code></pre>
  <pre class="lang-json prettyprint-override"><code>&quot;AzureAdB2C&quot;: {
      &quot;Instance&quot;: &quot;https://mydomain.b2clogin.com/&quot;,
      &quot;ClientId&quot;: &quot;60dbe9eb-056c-400d-a98d-c5c95b4bb176&quot;,
      &quot;Domain&quot;: &quot;mydomain.onmicrosoft.com&quot;,
      &quot;TenantId&quot;: &quot;1a5a2799-8dde-4236-901f-c37b3d2b9b39&quot;,
      &quot;Scopes&quot;: &quot;Data.Read&quot;,
      &quot;SignUpSignInPolicyId&quot;: &quot;B2C_1_susi&quot;,
      &quot;CallbackPath&quot;: &quot;/authentication/login-callback&quot;
  }
  </code></pre>
  <p><strong>Note:</strong> I have changed the guids and URLs for security reasons.</p>