Skip to content

Commit 6e0f3bb

Browse files
authored
Add the latest recommended passing tokens approach (#35482)
1 parent 21ba384 commit 6e0f3bb

6 files changed

Lines changed: 126 additions & 52 deletions

File tree

aspnetcore/blazor/call-web-api.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how to call a web API from Blazor apps.
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: wpickett
77
ms.custom: mvc
8-
ms.date: 04/29/2025
8+
ms.date: 05/30/2025
99
uid: blazor/call-web-api
1010
---
1111
# Call a web API from ASP.NET Core Blazor
@@ -33,7 +33,7 @@ For more information, see the following resources:
3333

3434
## Microsoft identity platform for web API calls
3535

36-
Blazor Web Apps that use use [Microsoft identity platform](/entra/identity-platform/)/[Microsoft Identity Web packages](/entra/msal/dotnet/microsoft-identity-web/) for [Microsoft Entra ID](https://www.microsoft.com/security/business/microsoft-entra) can make streamlined web API calls with API provided by the [`Microsoft.Identity.Web.DownstreamApi` NuGet package](https://www.nuget.org/packages/Microsoft.Identity.Web.DownstreamApi).
36+
Blazor Web Apps that use use [Microsoft identity platform](/entra/identity-platform/) with [Microsoft Identity Web packages](/entra/msal/dotnet/microsoft-identity-web/) for [Microsoft Entra ID](https://www.microsoft.com/security/business/microsoft-entra) can make streamlined web API calls with API provided by the [`Microsoft.Identity.Web.DownstreamApi` NuGet package](https://www.nuget.org/packages/Microsoft.Identity.Web.DownstreamApi).
3737

3838
[!INCLUDE[](~/includes/package-reference.md)]
3939

@@ -291,13 +291,13 @@ The solution includes a demonstration of obtaining weather data securely via an
291291
292292
### `BlazorWebAppEntra`
293293
294-
A Blazor Web App with global Auto interactivity that uses [Microsoft identity platform](/entra/identity-platform/)/[Microsoft Identity Web packages](/entra/msal/dotnet/microsoft-identity-web/) for [Microsoft Entra ID](https://www.microsoft.com/security/business/microsoft-entra). The solution includes a demonstration of obtaining weather data securely via an external web API when a component that adopts Interactive Auto rendering is rendered on the client.
294+
A Blazor Web App with global Auto interactivity that uses [Microsoft identity platform](/entra/identity-platform/) with [Microsoft Identity Web packages](/entra/msal/dotnet/microsoft-identity-web/) for [Microsoft Entra ID](https://www.microsoft.com/security/business/microsoft-entra). The solution includes a demonstration of obtaining weather data securely via an external web API when a component that adopts Interactive Auto rendering is rendered on the client.
295295
296296
### `BlazorWebAppEntraBff`
297297
298298
A Blazor Web App with global Auto interactivity that uses:
299299
300-
* [Microsoft identity platform](/entra/identity-platform/)/[Microsoft Identity Web packages](/entra/msal/dotnet/microsoft-identity-web/) for [Microsoft Entra ID](https://www.microsoft.com/security/business/microsoft-entra).
300+
* [Microsoft identity platform](/entra/identity-platform/) with [Microsoft Identity Web packages](/entra/msal/dotnet/microsoft-identity-web/) for [Microsoft Entra ID](https://www.microsoft.com/security/business/microsoft-entra).
301301
* The [Backend for Frontend (BFF) pattern](/azure/architecture/patterns/backends-for-frontends), which is a pattern of app development that creates backend services for frontend apps or interfaces.
302302
303303
The solution includes a demonstration of obtaining weather data securely via an external web API when a component that adopts Interactive Auto rendering is rendered on the client.
@@ -1120,10 +1120,10 @@ For a demonstration, see <xref:blazor/security/webassembly/standalone-with-ident
11201120
When composing an <xref:System.Net.Http.HttpRequestMessage>, set the browser request credentials and header directly:
11211121
11221122
```csharp
1123-
var requestMessage = new HttpRequestMessage() { ... };
1123+
var request = new HttpRequestMessage() { ... };
11241124
1125-
requestMessage.SetBrowserRequestCredentials(BrowserRequestCredentials.Include);
1126-
requestMessage.Headers.Add("X-Requested-With", [ "XMLHttpRequest" ]);
1125+
request.SetBrowserRequestCredentials(BrowserRequestCredentials.Include);
1126+
request.Headers.Add("X-Requested-With", [ "XMLHttpRequest" ]);
11271127
```
11281128
11291129
## `HttpClient` and `HttpRequestMessage` with Fetch API request options
@@ -1156,7 +1156,7 @@ requestMessage.Headers.Add("X-Requested-With", [ "XMLHttpRequest" ]);
11561156
11571157
private async Task PostRequest()
11581158
{
1159-
var requestMessage = new HttpRequestMessage()
1159+
var request = new HttpRequestMessage()
11601160
{
11611161
Method = new HttpMethod("POST"),
11621162
RequestUri = new Uri("https://localhost:10000/todoitems"),
@@ -1172,13 +1172,13 @@ requestMessage.Headers.Add("X-Requested-With", [ "XMLHttpRequest" ]);
11721172
11731173
if (tokenResult.TryGetToken(out var token))
11741174
{
1175-
requestMessage.Headers.Authorization =
1175+
request.Headers.Authorization =
11761176
new AuthenticationHeaderValue("Bearer", token.Value);
11771177
1178-
requestMessage.Content.Headers.TryAddWithoutValidation(
1178+
request.Content.Headers.TryAddWithoutValidation(
11791179
"x-custom-header", "value");
11801180
1181-
var response = await Http.SendAsync(requestMessage);
1181+
var response = await Http.SendAsync(request);
11821182
var responseStatusCode = response.StatusCode;
11831183
11841184
responseBody = await response.Content.ReadAsStringAsync();
@@ -1222,10 +1222,10 @@ To opt-out of response streaming globally, use either of the following approache
12221222
12231223
To opt-out of response streaming globally, set the `DOTNET_WASM_ENABLE_STREAMING_RESPONSE` environment variable to `false` or `0`.
12241224
1225-
To opt-out of response streaming for an individual request, set <xref:Microsoft.AspNetCore.Components.WebAssembly.Http.WebAssemblyHttpRequestMessageExtensions.SetBrowserResponseStreamingEnabled%2A> to `false` on the <xref:System.Net.Http.HttpRequestMessage> (`requestMessage` in the following example):
1225+
To opt-out of response streaming for an individual request, set <xref:Microsoft.AspNetCore.Components.WebAssembly.Http.WebAssemblyHttpRequestMessageExtensions.SetBrowserResponseStreamingEnabled%2A> to `false` on the <xref:System.Net.Http.HttpRequestMessage> (`request` in the following example):
12261226
12271227
```csharp
1228-
requestMessage.SetBrowserResponseStreamingEnabled(false);
1228+
request.SetBrowserResponseStreamingEnabled(false);
12291229
```
12301230
12311231
:::moniker-end
@@ -1235,22 +1235,22 @@ requestMessage.SetBrowserResponseStreamingEnabled(false);
12351235
The HTTP response is typically buffered to enable support for synchronous reads on the response content. To enable support for response streaming, set <xref:Microsoft.AspNetCore.Components.WebAssembly.Http.WebAssemblyHttpRequestMessageExtensions.SetBrowserResponseStreamingEnabled%2A> to `true` on the <xref:System.Net.Http.HttpRequestMessage>:
12361236
12371237
```csharp
1238-
requestMessage.SetBrowserResponseStreamingEnabled(true);
1238+
request.SetBrowserResponseStreamingEnabled(true);
12391239
```
12401240
12411241
By default, [`HttpCompletionOption.ResponseContentRead`](xref:System.Net.Http.HttpCompletionOption) is set, which results in the <xref:System.Net.Http.HttpClient> completing after reading the entire response, including the content. In order to be able to use the <xref:Microsoft.AspNetCore.Components.WebAssembly.Http.WebAssemblyHttpRequestMessageExtensions.SetBrowserResponseStreamingEnabled%2A> option on large files, set [`HttpCompletionOption.ResponseHeadersRead`](xref:System.Net.Http.HttpCompletionOption) to avoid caching the file's content in memory:
12421242
12431243
```diff
1244-
- var response = await Http.SendAsync(requestMessage);
1245-
+ var response = await Http.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead);
1244+
- var response = await Http.SendAsync(request);
1245+
+ var response = await Http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
12461246
```
12471247
12481248
:::moniker-end
12491249
12501250
To include credentials in a cross-origin request, use the <xref:Microsoft.AspNetCore.Components.WebAssembly.Http.WebAssemblyHttpRequestMessageExtensions.SetBrowserRequestCredentials%2A> extension method:
12511251
12521252
```csharp
1253-
requestMessage.SetBrowserRequestCredentials(BrowserRequestCredentials.Include);
1253+
request.SetBrowserRequestCredentials(BrowserRequestCredentials.Include);
12541254
```
12551255
12561256
For more information on Fetch API options, see [MDN web docs: WindowOrWorkerGlobalScope.fetch(): Parameters](https://developer.mozilla.org/docs/Web/API/fetch#Parameters).

0 commit comments

Comments
 (0)