Skip to content

Commit b411e6f

Browse files
authored
Merge pull request #35364 from dotnet/main
2 parents 7f2377e + b217625 commit b411e6f

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

aspnetcore/blazor/call-web-api.md

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,69 @@ 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 calls using Entra-specific API. This approach is used by the `BlazorWebAppEntra` and `BlazorWebAppEntraBff` sample apps described in the *Sample apps* section of this article.
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).
37+
38+
[!INCLUDE[](~/includes/package-reference.md)]
39+
40+
In the app settings file (`appsettings.json`), provide a base URL and scopes. In the following example, the `{BASE ADDRESS}` placeholder is the base URL of the web API. A single scope is specified with an App ID URI (`{APP ID URI}` placeholder) and scope name (`{SCOPE NAME}` placeholder):
41+
42+
```json
43+
"DownstreamApi": {
44+
"BaseUrl": "{BASE ADDRESS}",
45+
"Scopes": [ "{APP ID URI}/{SCOPE NAME}" ]
46+
}
47+
```
48+
49+
Example:
50+
51+
```json
52+
"DownstreamApi": {
53+
"BaseUrl": "https://localhost:7277",
54+
"Scopes": [ "api://11112222-bbbb-3333-cccc-4444dddd5555/Weather.Get" ]
55+
}
56+
```
57+
58+
In the app's `Program` file, call:
59+
60+
<!-- UPDATE 10.0 - Missing API doc for 'AddDownstreamApi' -->
61+
62+
* <xref:Microsoft.Identity.Web.MicrosoftIdentityWebApiAuthenticationBuilder.EnableTokenAcquisitionToCallDownstreamApi%2A>: Enables token acquisition to call web APIs.
63+
* `AddDownstreamApi`: Adds a named downstream web service related to a specific configuration section.
64+
* <xref:Microsoft.Identity.Web.TokenCacheProviders.InMemory.InMemoryTokenCacheProviderExtension.AddInMemoryTokenCaches%2A>: Adds both the app and per-user in-memory token caches.
65+
66+
```csharp
67+
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
68+
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"))
69+
.EnableTokenAcquisitionToCallDownstreamApi()
70+
.AddDownstreamApi("DownstreamApi", builder.Configuration.GetSection("DownstreamApi"))
71+
.AddInMemoryTokenCaches();
72+
```
73+
74+
Inject <xref:Microsoft.Identity.Abstractions.IDownstreamApi> and call <xref:Microsoft.Identity.Abstractions.IDownstreamApi.CallApiForUserAsync%2A> when calling on behalf of a user:
75+
76+
```csharp
77+
internal sealed class ServerWeatherForecaster(IDownstreamApi downstreamApi) : IWeatherForecaster
78+
{
79+
public async Task<IEnumerable<WeatherForecast>> GetWeatherForecastAsync()
80+
{
81+
var response = await downstreamApi.CallApiForUserAsync("DownstreamApi",
82+
options =>
83+
{
84+
options.RelativePath = "/weather-forecast";
85+
});
86+
87+
return await response.Content.ReadFromJsonAsync<WeatherForecast[]>() ??
88+
throw new IOException("No weather forecast!");
89+
}
90+
}
91+
```
92+
93+
This approach is used by the `BlazorWebAppEntra` and `BlazorWebAppEntraBff` sample apps described in the *Sample apps* section of this article.
3794

3895
For more information, see the following resources:
3996

40-
* <xref:blazor/security/additional-scenarios#use-a-token-handler-for-web-api-calls>
97+
* [Web API documentation | Microsoft identity platform](/entra/identity-platform/index-web-api)
98+
* <xref:Microsoft.Identity.Abstractions.IDownstreamApi>
4199
* *Secure an ASP.NET Core Blazor Web App with Microsoft Entra ID*
42100
* [Non-BFF pattern (Interactive Auto)](xref:blazor/security/blazor-web-app-entra?pivots=non-bff-pattern)
43101
* [BFF pattern (Interactive Auto)](xref:blazor/security/blazor-web-app-entra?pivots=non-bff-pattern-server)

0 commit comments

Comments
 (0)