question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Server-side Blazor: HttpContext is null when new component is created

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
mihaimyhcommented, Dec 27, 2019

This still is an issue, I can believe there is no official support for this yet since Authentication/Authorization should be considered a priority.

0reactions
pranavkmcommented, Dec 30, 2019

@javiercn looks like this hasn’t been resolved as yet - https://github.com/aspnet/AspNetCore/issues/18066. Can you help?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found