Skip to content

Commit 8e88a99

Browse files
authored
Additional ServerWeatherForecaster scenarios (#35347)
1 parent 2b3283b commit 8e88a99

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

aspnetcore/blazor/call-web-api.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,25 @@ The [`System.Net.Http.Json`](https://www.nuget.org/packages/System.Net.Http.Json
2222

2323
## Use a token handler for web API calls
2424

25-
Blazor Web Apps with OIDC authentication can use a token handler approach to make outgoing requests to secure external web API calls. This approach is used by the `BlazorWebAppOidc` and `BlazorWebAppOidcServer` sample apps described in the next section.
25+
Blazor Web Apps with OIDC authentication can use a token handler approach to make outgoing requests to secure external web API calls. This approach is used by the `BlazorWebAppOidc` and `BlazorWebAppOidcServer` sample apps described in the *Sample apps* section of this article.
2626

2727
For more information, see the following resources:
2828

2929
* <xref:blazor/security/additional-scenarios#use-a-token-handler-for-web-api-calls>
3030
* *Secure an ASP.NET Core Blazor Web App with OpenID Connect (OIDC)*
31-
* [Non-BFF pattern (Interactive Auto)](xref:blazor/security/blazor-web-app-oidc?view=aspnetcore-9.0&pivots=non-bff-pattern)
32-
* [Non-BFF pattern (Interactive Server)](xref:blazor/security/blazor-web-app-oidc?view=aspnetcore-9.0&pivots=non-bff-pattern-server)
31+
* [Non-BFF pattern (Interactive Auto)](xref:blazor/security/blazor-web-app-oidc?pivots=non-bff-pattern)
32+
* [Non-BFF pattern (Interactive Server)](xref:blazor/security/blazor-web-app-oidc?pivots=non-bff-pattern-server)
33+
34+
## Microsoft identity platform for web API calls
35+
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.
37+
38+
For more information, see the following resources:
39+
40+
* <xref:blazor/security/additional-scenarios#use-a-token-handler-for-web-api-calls>
41+
* *Secure an ASP.NET Core Blazor Web App with Microsoft Entra ID*
42+
* [Non-BFF pattern (Interactive Auto)](xref:blazor/security/blazor-web-app-entra?pivots=non-bff-pattern)
43+
* [BFF pattern (Interactive Auto)](xref:blazor/security/blazor-web-app-entra?pivots=non-bff-pattern-server)
3344

3445
## Sample apps
3546

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ The following specification is covered:
3030
* The app uses [Microsoft Entra ID](https://www.microsoft.com/security/business/microsoft-entra), based on [Microsoft Identity Web](/entra/msal/dotnet/microsoft-identity-web/) packages.
3131
* Automatic non-interactive token refresh is managed by the framework.
3232
* The app uses server-side and client-side service abstractions to display generated weather data:
33-
* When rendering the `Weather` component on the server to display weather data, the component uses the `ServerWeatherForecaster` on the server to directly obtain weather data (not via a web API call).
33+
* When rendering the `Weather` component on the server to display weather data, the component uses the `ServerWeatherForecaster` on the server to obtain weather data.
3434
* When the `Weather` component is rendered on the client, the component uses the `ClientWeatherForecaster` service implementation, which uses a preconfigured <xref:System.Net.Http.HttpClient> (in the client project's `Program` file) to make a web API call to the server project's Minimal API (`/weather-forecast`) for weather data. The Minimal API endpoint obtains the weather data from the `ServerWeatherForecaster` class and returns it to the client for rendering by the component.
3535

3636
## Sample solution

aspnetcore/blazor/security/index.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ else
772772
The server project implements `IWeatherForecaster` as `ServerWeatherForecaster`, which generates and returns mock weather data via its `GetWeatherForecastAsync` method:
773773

774774
```csharp
775-
public class ServerWeatherForecaster() : IWeatherForecaster
775+
internal sealed class ServerWeatherForecaster() : IWeatherForecaster
776776
{
777777
public readonly string[] summaries =
778778
[
@@ -797,6 +797,45 @@ public class ServerWeatherForecaster() : IWeatherForecaster
797797
}
798798
```
799799

800+
Alternatively, the `ServerWeatherForecaster` can call an external web API using a [named HTTP Client and token handler approach](xref:blazor/call-web-api#use-a-token-handler-for-web-API-calls), as the following example demonstrates:
801+
802+
```csharp
803+
internal sealed class ServerWeatherForecaster(IHttpClientFactory clientFactory) : IWeatherForecaster
804+
{
805+
public async Task<IEnumerable<WeatherForecast>> GetWeatherForecastAsync()
806+
{
807+
var request = new HttpRequestMessage(HttpMethod.Get, "/weather-forecast");
808+
var client = clientFactory.CreateClient("ExternalApi");
809+
810+
var response = await client.SendAsync(request);
811+
812+
response.EnsureSuccessStatusCode();
813+
814+
return await response.Content.ReadFromJsonAsync<WeatherForecast[]>() ??
815+
throw new IOException("No weather forecast!");
816+
}
817+
}
818+
```
819+
820+
If the app 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) (see <xref:blazor/call-web-api#microsoft-identity-platform-for-web-api-calls>), the `ServerWeatherForecaster` might appear like the following class to make external web API calls:
821+
822+
```csharp
823+
internal sealed class ServerWeatherForecaster(IDownstreamApi downstreamApi) : IWeatherForecaster
824+
{
825+
public async Task<IEnumerable<WeatherForecast>> GetWeatherForecastAsync()
826+
{
827+
var response = await downstreamApi.CallApiForUserAsync("DownstreamApi",
828+
options =>
829+
{
830+
options.RelativePath = "/weather-forecast";
831+
});
832+
833+
return await response.Content.ReadFromJsonAsync<WeatherForecast[]>() ??
834+
throw new IOException("No weather forecast!");
835+
}
836+
}
837+
```
838+
800839
The server project maintains a secure web API endpoint for client weather data calls:
801840

802841
```csharp

0 commit comments

Comments
 (0)