Skip to content

Commit 87253b0

Browse files
authored
Update example for NRTs + cross-linking (#35554)
1 parent 75926d2 commit 87253b0

3 files changed

Lines changed: 13 additions & 4 deletions

File tree

aspnetcore/blazor/call-web-api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,9 @@ public class CookieHandler : DelegatingHandler
10951095
}
10961096
```
10971097
1098+
> [!NOTE]
1099+
> For guidance on how to access an `AuthenticationStateProvider` from a `DelegatingHandler`, see <xref:blazor/security/additional-scenarios#access-authenticationstateprovider-in-outgoing-request-middleware>.
1100+
10981101
The `CookieHandler` is registered in the `Program` file:
10991102
11001103
```csharp

aspnetcore/blazor/security/additional-scenarios.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public class TokenHandler(IHttpContextAccessor httpContextAccessor) :
7070
}
7171
```
7272

73+
> [!NOTE]
74+
> For guidance on how to access an `AuthenticationStateProvider` from a `DelegatingHandler`, see the [Access `AuthenticationStateProvider` in outgoing request middleware](#access-authenticationstateprovider-in-outgoing-request-middleware) section.
75+
7376
In the project's `Program` file, the token handler (`TokenHandler`) is registered as a scoped service and specified as a [named HTTP client's](xref:blazor/call-web-api#named-httpclient-with-ihttpclientfactory) message handler with <xref:Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.AddHttpMessageHandler%2A>.
7477

7578
In the following example, the `{HTTP CLIENT NAME}` placeholder is the name of the <xref:System.Net.Http.HttpClient>, and the `{BASE ADDRESS}` placeholder is the web API's base address URI.
@@ -789,19 +792,21 @@ Use the `CircuitServicesAccessor` to access the <xref:Microsoft.AspNetCore.Compo
789792
`AuthenticationStateHandler.cs`:
790793

791794
```csharp
795+
using Microsoft.AspNetCore.Components.Authorization;
796+
792797
public class AuthenticationStateHandler(
793798
CircuitServicesAccessor circuitServicesAccessor)
794799
: DelegatingHandler
795800
{
796801
protected override async Task<HttpResponseMessage> SendAsync(
797802
HttpRequestMessage request, CancellationToken cancellationToken)
798803
{
799-
var authStateProvider = circuitServicesAccessor.Services
804+
var authStateProvider = circuitServicesAccessor.Services?
800805
.GetRequiredService<AuthenticationStateProvider>();
801-
var authState = await authStateProvider.GetAuthenticationStateAsync();
802-
var user = authState.User;
806+
var authState = authStateProvider?.GetAuthenticationStateAsync().Result;
807+
var user = authState?.User;
803808

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

aspnetcore/blazor/security/blazor-web-app-with-oidc.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,3 +1435,4 @@ At this point, Razor components can adopt [role-based and policy-based authoriza
14351435
* [Manage authentication state in Blazor Web Apps](xref:blazor/security/index#manage-authentication-state-in-blazor-web-apps)
14361436
* [Refresh token during http request in Blazor Interactive Server with OIDC (`dotnet/aspnetcore` #55213)](https://github.com/dotnet/aspnetcore/issues/55213)
14371437
* [Secure data in Blazor Web Apps with Interactive Auto rendering](xref:blazor/security/index#secure-data-in-blazor-web-apps-with-interactive-auto-rendering)
1438+
* [How to access an `AuthenticationStateProvider` from a `DelegatingHandler`](xref:blazor/security/additional-scenarios#access-authenticationstateprovider-in-outgoing-request-middleware)

0 commit comments

Comments
 (0)