Skip to content

Blazor Server - HttpContextAccessor.HttpContext null on Linux, but accessable on windows. #62351

Closed
@Borgling

Description

@Borgling

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Using .NET 9 Blazor Server I get unexpected behaviour between Windows and Linux hosting.

Currently I'm getting an email address from the initial request headers. I've tried various ways of doing it, and always get the same issue. Works on Windows, doesn't work on Linux.

Here is my latest example of the code, although this is 1 of many different approaches all with the same issue (works on windows not on Linux):

    public override Task OnConnectionUpAsync(Circuit circuit, CancellationToken cancellationToken)
    {
        Console.WriteLine($"HttpContextAccessor: {_httpContextAccessor}, HttpContext null: {_httpContextAccessor.HttpContext == null}, Request null: {_httpContextAccessor.HttpContext?.Request == null}, Headers null: {_httpContextAccessor.HttpContext?.Request?.Headers == null}");

        var headers = _httpContextAccessor.HttpContext?.Request?.Headers;

        if (headers != null)
        {
            var headerDict = headers.ToDictionary(h => h.Key, h => string.Join(", ", h.Value.ToArray()));
            _headerStore.SetHeaders(circuit.Id, headerDict);
            Console.WriteLine(string.Join(", ", headers.ToArray()));


        }

        return Task.CompletedTask;
    }

On windows 10, running the published .exe, it runs on http://localhost:5000

If I curl http://localhost:5000 I get the expected output:

HttpContextAccessor: Microsoft.AspNetCore.Http.HttpContextAccessor, HttpContext null: False, Request null: False, Headers null: False
[Connection, Upgrade], [Host, localhost:5000], [User-Agent, ..... etc etc

But running on Linux, using dotnet myapp.dll followed by curl http://localhost:5000 I will get the following output:

HttpContextAccessor: Microsoft.AspNetCore.Http.HttpContextAccessor, HttpContext null: True, Request null: True, Headers null: True

Showing that HttpContextAccessor.HttpContext is null

I have used the following code, and headers are sent correctly, on both windows and linux.


app.Use(async (context, next) =>
{
    if (context.Request.Path.StartsWithSegments("/_blazor/negotiate"))
    {
        // capture headers (e.g. save in a temp store keyed by connection ID or cookie)
        Console.WriteLine("Negotiation headers:");
        foreach (var header in context.Request.Headers)
        {
            Console.WriteLine($"{header.Key} = {header.Value}");
        }
    }

    await next();
});

Technically I think HttpContextAccessor.HttpContext should be null after the initial request, as we move onto the SignalR connection.

But I can't understand why it's accessible on windows but not linux.

Also then leaves us with the questions, on Linux, how can you map the initial request headers, to be accessible within the SignalR connection scope.

Expected Behavior

The same behaviour between Kestrel running on windows, and Kestrel running on Linux.

Steps To Reproduce

A simple test that works on windows but not Linux.

@page "/"

@inject IHttpContextAccessor accessor
<h3>Your Request Headers</h3>

@if (accessor.HttpContext == null)
{
    <p><em>No headers found.</em></p>
}
else
{
    <ul>
        @foreach (var header in accessor.HttpContext.Request.Headers)
        {
            <li><strong>@header.Key</strong>: @header.Value</li>
        }
    </ul>
}

Exceptions (if any)

No response

.NET Version

No response

Anything else?

No response

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