Skip to content

Update example for NRTs #35552

@guardrex

Description

@guardrex

Description

Updates for NRT ...

using Microsoft.AspNetCore.Components.Authorization;

namespace BlazorWebAppOidc;

public class AuthenticationStateHandler(
    CircuitServicesAccessor circuitServicesAccessor)
    : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var authStateProvider = circuitServicesAccessor.Services?
            .GetRequiredService<AuthenticationStateProvider>();
        var authState = authStateProvider?.GetAuthenticationStateAsync().Result;
        var user = authState?.User;

        if (user?.Identity is not null && user.Identity.IsAuthenticated)
        {
            request.Headers.Add("X-USER-IDENTITY-NAME", user.Identity.Name);
        }

        return await base.SendAsync(request, cancellationToken);
    }
}

Also ...

This section ...

https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/dependency-injection?view=aspnetcore-7.0#access-server-side-blazor-services-from-a-different-di-scope

... is relevant for HTTP Client, so better cross-link it into the articles where it has a bearing, such as the Call web APIs article.

Page URL

https://learn.microsoft.com/en-us/aspnet/core/blazor/security/additional-scenarios?view=aspnetcore-8.0#access-authenticationstateprovider-in-outgoing-request-middleware

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/blazor/security/additional-scenarios.md

Document ID

85d1072a-59eb-7b01-2476-fa699ee03653

Platform Id

0bb44a31-eea5-7fdc-3d77-85dbf2f31315

Article author

@guardrex

Metadata

  • ID: 85d1072a-59eb-7b01-2476-fa699ee03653
  • PlatformId: 0bb44a31-eea5-7fdc-3d77-85dbf2f31315
  • Service: aspnet-core
  • Sub-service: blazor

Related Issues

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions