You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/blazor/security/additional-scenarios.md
+249-1Lines changed: 249 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,7 +37,7 @@ Note that <xref:Microsoft.AspNetCore.Http.HttpContext> used as a [cascading para
37
37
38
38
For more information, see <xref:blazor/components/httpcontext>.
39
39
40
-
### Example
40
+
### Token handler example for web API calls
41
41
42
42
The following approach is aimed at attaching a user's access token to outgoing requests, specifically to make web API calls to external web API apps. The approach is shown for a Blazor Web App that adopts global Interactive Server rendering, but the same general approach applies to Blazor Web Apps that adopt the global Interactive Auto render mode. The important concept to keep in mind is that accessing the <xref:Microsoft.AspNetCore.Http.HttpContext> using <xref:Microsoft.AspNetCore.Http.IHttpContextAccessor> is only performed during static SSR.
43
43
@@ -124,6 +124,254 @@ var response = await client.SendAsync(request);
124
124
125
125
Additional features are planned for Blazor, which are tracked by [Access `AuthenticationStateProvider` in outgoing request middleware (`dotnet/aspnetcore`#52379)](https://github.com/dotnet/aspnetcore/issues/52379). [Problem providing Access Token to HttpClient in Interactive Server mode (`dotnet/aspnetcore`#52390)](https://github.com/dotnet/aspnetcore/issues/52390) is a closed issue that contains helpful discussion and potential workaround strategies for advanced use cases.
126
126
127
+
### Root-level cascading values with notifications
128
+
129
+
Tokens can be passed via [root-level cascading values](xref:blazor/components/cascading-values-and-parameters#root-level-cascading-values) with a <xref:Microsoft.AspNetCore.Components.CascadingValueSource%601> with subscriber notifications. This general approach works well when you need to interact with tokens outside of calling a web API.
130
+
131
+
The following `CascadingStateServiceCollectionExtensions` creates a <xref:Microsoft.AspNetCore.Components.CascadingValueSource%601> from a type that implements <xref:System.ComponentModel.INotifyPropertyChanged>.
132
+
133
+
> [!NOTE]
134
+
> For Blazor Web App solutions consisting of server and client (`.Client`) projects, place the following `CascadingStateServiceCollectionExtensions.cs` file into the `.Client` project.
Create a class to manage the token state. The following `NotifyingState` example tracks state for access and refresh tokens.
184
+
185
+
> [!NOTE]
186
+
> For Blazor Web App solutions consisting of server and client (`.Client`) projects, place the following `NotifyingState.cs` file into the `.Client` project.
> †For Blazor Web App solutions consisting of server and client (`.Client`) projects, place the preceding code into each project's `Program` file.
242
+
243
+
The `Program` file of the server project must also register <xref:Microsoft.AspNetCore.Http.IHttpContextAccessor>:
244
+
245
+
```csharp
246
+
builder.Services.AddHttpContextAccessor();
247
+
```
248
+
249
+
At the top of the `App` component (`App.razor`), add an [`@using`](xref:mvc/views/razor#using) directive for <xref:Microsoft.AspNetCore.Authentication?displayProperty=fullName>:
250
+
251
+
```razor
252
+
@using Microsoft.AspNetCore.Authentication
253
+
```
254
+
255
+
Under the markup of the `App` component, add the following `@code` block to set the `AccessToken` and `RefreshToken` properties from the cascaded <xref:Microsoft.AspNetCore.Http.HttpContext>. The following example is suitable when using an OIDC identity provider, an example for Microsoft Entra ID with Microsoft Identity Web packages follows this example.
The app, including within components, can now obtain the access and refresh tokens, keeping in mind that the values are set for the life of the circuit unless developer code updates them. The following approach is effective in server-side scenarios, and an example follows that's useful in Blazor Web Apps that adopt Interactive Auto rendering.
309
+
310
+
```csharp
311
+
privatestring?accessToken;
312
+
privatestring?refreshToken;
313
+
314
+
[CascadingParameter]
315
+
publicNotifyingState?State { get; set; }
316
+
317
+
protectedoverrideasyncTaskOnInitializedAsync()
318
+
{
319
+
accessToken=State?.AccessToken;
320
+
refreshToken=State?.RefreshToken;
321
+
}
322
+
```
323
+
324
+
The following example demonstrates the concept in an app that adopts Interactive Auto rendering with server and `.Client` projects, where the tokens must be persisted from the server during prerendering:
### Passing the anti-request forgery (CSRF/XSRF) token
128
376
129
377
Passing the [anti-request forgery (CSRF/XSRF) token](xref:security/anti-request-forgery) to Razor components is useful in scenarios where components POST to Identity or other endpoints that require validation. However, don't follow the guidance in this section for processing form POST requests or web API requests with XSRF support. The Blazor framework provides built-in antiforgery support for forms and calling web APIs. For more information, see the following resources:
0 commit comments