Server-side Blazor: HttpContext is null when new component is created
See original GitHub issueI’ve got a server-side Blazor / IdentityServer4 / API setup. With every request to the API I’ve got to send the access token that I got from the identity server. I get the access token from the HttpContext when my HttpClient gets created in Startup.cs:
services.AddHttpClient<IMyClient, MyClient>(client =>
{
var serviceProvider = services.BuildServiceProvider();
var httpContextAccessor = serviceProvider.GetService<IHttpContextAccessor>();
var task = httpContextAccessor.HttpContext.GetTokenAsync("access_token");
task.Wait();
var accessToken = task.Result;
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
client.BaseAddress = new Uri(Configuration.GetMyApiServerUrl());
});
Every component, that has to make calls to the API, injects MyClient
. If such a component is created, the AddHttpClient
function is called and the access token is set for future calls to the API.
This works well on my local machine but on the server, only the first call after page load works. After that, if a new component gets created that injects MyClient
, the HttpContext is null inside the AddHttpClient
function.
Any idea on how to solve this?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:11 (6 by maintainers)
Top Results From Across the Web
Blazor Server HttpContext is null when published on local IIS
1 Answer. You shouldn't use HttpContextAccessor in Blazor Server because the Blazor Server works outside the . NetCore pipeline and basically ...
Read more >Server-side Blazor: HttpContext is null when new ...
I get the access token from the HttpContext when my HttpClient gets created in Startup.cs: services.AddHttpClient<IMyClient, MyClient>(client => ...
Read more >Blazor Server IHttpContextAccessor returning null when ...
This is working perfectly when running locally/debugging but when its deployed it's returning a null reference exception.
Read more >Using the HttpContext in Blazor Server the right way - YouTube
Creating services and initial application state: 17:20 6. ... Passing captured HttpContext state to the Blazor root component : 22:29 8.
Read more >How do you use the HttpContext object in Blazor server- ...
Use HttpContext through the IHttpContextAccessor interface to get the user agent details in a Blazor Server application. The following example demonstrates ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This still is an issue, I can believe there is no official support for this yet since Authentication/Authorization should be considered a priority.
@javiercn looks like this hasn’t been resolved as yet - https://github.com/aspnet/AspNetCore/issues/18066. Can you help?