Skip to content

HttpContext in Blazor Web App .net 8 Render Interactive Auto Stay null in production #62948

@imprathamesh

Description

@imprathamesh

Hello,
I have created project in Blazor Web App .Net 8 Render Interactive Auto.
I am authorizing the app via idp

so sequence of process goes like this:

  1. visit client app
  2. Redirect to idp provider
  3. Successfully login created access token and claims
  4. Redirect to client app with access token
  5. DelegatingHandler as AuthorizationHandler in Server Side. It used to add Access token into data Header to access the secured api controllers. So I use httpContextAccessor (HttpContext)
  6. here it throwing the errro HttpContext is null.
  7. in browser console showing WebSocket has issue.

and this issue I only facing in production. In development it works fine.

I refer the authorization from

https://github.com/mohaaron/CustomAuthorizationHandler
BlazorWebApp/Services/ServerTokenHandler.cs this is the path . I named AuthorizationHandler.

Here is my code, please check-

`public class AuthorizationHandler(IHttpContextAccessor httpContextAccessor) : DelegatingHandler

{

private readonly IHttpContextAccessor _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor));

protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)

{

var httpContext = _httpContextAccessor.HttpContext ??

    throw new InvalidOperationException("No HttpContext available from the IHttpContextAccessor!");

if (httpContext != null && httpContext.Request.Cookies.TryGetValue("access_token", out var token))

{

    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);

}

return await base.SendAsync(request, cancellationToken);

} }

services.AddTransient<AuthorizationHandler>();

 var httpClientBuilder = services.AddHttpClient("ApiClient", client =>

 { client.BaseAddress = new Uri("https://localhost:7067/"); }).AddHttpMessageHandler<AuthorizationHandler>();   

`
Please help me upon this, this is really exhausting when it working in development and not in production.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-blazorIncludes: Blazor, Razor Components

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions