Skip to content

Blazor Web App - HTTP headers missing from inside a component when using IHttpContextAccessor #53192

@damienbod

Description

@damienbod

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I need to read the HTTP headers from inside a Blazor Web component.

When using the IHttpContextAccessor, the headers are not returned even though the headers exist in all the HTTP responses.

How can I use the HTTP headers returned in the client responses when using Blazor Web?

Expected Behavior

All headers from the HTTP response should be present in the IHttpContextAccessor

@inject IHostEnvironment Env
@inject IHttpContextAccessor HttpContextAccessor

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <base href="/" />
    <link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
    <link href="css/site.css" rel="stylesheet" />
    <link href="BlazorWebFromBlazorServerOidc.styles.css" rel="stylesheet" />
    <HeadOutlet @rendermode="InteractiveServer" />
</head>
<body>
    <Routes @rendermode="InteractiveServer" />

    <script nonce="@GetNonce()" src="_framework/blazor.web.js"></script>
</body>
</html>

@code {

    private string GetNonce()
    {
        var nonce = string.Empty;
        Microsoft.Extensions.Primitives.StringValues nonces;
        var found = HttpContextAccessor.HttpContext!.Response.Headers.TryGetValue("Content-Security-Policy", out nonces);

        if(found)
        {
            nonce = nonces.First();
        };

        return nonce!;
    }
}

.NET Version

8

Anything else?

Blazor Web (Server)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions