Blazor wasm: Services.AddHttpClient with AddHttpMessageHandler and IHttpClientFactory

94 views Asked by At

in my blazor Net7 application I configure these services for add jwt token in request header (AuthenticationHandler).

builder.Services.AddHttpClient("ServerApi")
            .ConfigureHttpClient(c => c.BaseAddress = new Uri(builder.Configuration["WebApi"] ?? ""))
            .AddHttpMessageHandler<AuthenticationHandler>();

builder.Services.AddTransient(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("ServerApi"));

I would expect that when I inject an object of type IHttpClientFactory and with it create an httpClient object of name "ServerApi" (_factory.CreateClient("ServerApi").GetAsync(...)) that the http request have the authorizazion header, added by the handler AuthenticationHandler. But this doesn't happen.

Any suggestion? Thank you

0

There are 0 answers