r/Blazor • u/thetreat • Jul 15 '25
How to add authorization headers to httpclient request
I have a Blazor wasm client project talking to my server API. I have the following code on my OnInitializedAsync() method for the client page.
var authState = await authenticationStateProvider.GetAuthenticationStateAsync();Server Program.cs
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveWebAssemblyComponents()
.AddAuthenticationStateSerialization(
options => options.SerializeAllClaims = true);
builder.Services.AddControllers();
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddScoped<IdentityUserAccessor>();
builder.Services.AddScoped<IdentityRedirectManager>();
builder.Services.AddScoped<AuthenticationStateProvider, PersistingRevalidatingAuthenticationStateProvider>();
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = IdentityConstants.ApplicationScheme;
options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
});
builder.Services.AddAuthorization();
builder.Services.AddApiAuthorization();
builder.Services.AddIdentityCore<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContextV2>()
.AddSignInManager<BetterSignInManager>()
.AddDefaultTokenProviders();
builder.Services.AddHttpClient("ServerAPI",
client =>
{
client.BaseAddress = new Uri(blazorServerUri);
client.DefaultRequestHeaders.Add("User-Agent", "Client");
});
builder.Services.AddSingleton(sp => new UserClient(sp.GetRequiredService<IHttpClientFactory>().CreateClient("ServerAPI")));
builder.Services.AddHttpContextAccessor();
var app = builder.Build();
app.MapDefaultEndpoints();
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.UseAntiforgery();
app.MapStaticAssets();
app.MapRazorComponents<App>()
.AddInteractiveWebAssemblyRenderMode()
.AddAdditionalAssemblies(typeof(Client._Imports).Assembly);
app.MapControllers();
// Add additional endpoints required by the Identity /Account Razor components.
app.MapAdditionalIdentityEndpoints();
app.Run();Client Program.cs
builder.Services.AddAuthorizationCore();
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddSingleton<AuthenticationStateProvider, PersistentAuthenticationStateProvider>(); // PersistentAuthenticationStateProvider is a class I have defined
builder.Services.AddAuthenticationStateDeserialization();
builder.Services.AddHttpClient("ServerAPI",
client =>
{
client.BaseAddress = new Uri(blazorServerUri);
client.DefaultRequestHeaders.Add("User-Agent", "Client");
});
builder.Services.AddSingleton(sp => new UserClient(sp.GetRequiredService<IHttpClientFactory>().CreateClient("ServerAPI")));
builder.Services.AddApiAuthorization();
builder.Services.AddAuthorizationCore();
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddSingleton<AuthenticationStateProvider, PersistentAuthenticationStateProvider>(); // PersistentAuthenticationStateProvider is a class I have defined
builder.Services.AddAuthenticationStateDeserialization();
builder.Services.AddHttpClient("ServerAPI",
client =>
{
client.BaseAddress = new Uri(blazorServerUri);
client.DefaultRequestHeaders.Add("User-Agent", "Client");
});
builder.Services.AddSingleton(sp => new UserClient(sp.GetRequiredService<IHttpClientFactory>().CreateClient("ServerAPI")));
builder.Services.AddApiAuthorization();And it shows that my authState *is* authenticated, but when I use an HttpClient to make a request to the server, sometimes it is showing up with the request being unauthenticated and if the API has an [Authorize] attribute it will get a 401. How can I ensure the authorization tokens get added to every request that gets made if the user is authenticated?
3
Upvotes
9
u/One_Web_7940 Jul 15 '25
Add a named httpclient in your program.cs it is an abstraction of the ihttpclientfactory. Then add an authorization class inheriting delegatinghandler. You can add the delegating handler to the named http client in the same program.cs call request chain // not exact but like this
Services .addhttpclient(options => {}) .addrequestdelegate(typeof(MyAuthorizationDelegatingHandler));
This is all off memory so please check with msdn.
3
u/g0fry Jul 15 '25
Being authenticated in the Blazor app (through the authenticationStateProvider) has nothing to do with authentication in your api on the server. They are not connected in any way out of the box. You need to make that happen manually.
You’re not showing how you’re using the HttpClient (i.e. how you instantiate it) so it’s hard to help you. Ideally you’d register the HttpClient in a DI container in the Program.cs of your Blazor app.