From 1af2e6ab8f152128e5594df95ac464f28b89da44 Mon Sep 17 00:00:00 2001 From: Luke Latham Date: Thu, 9 Jul 2020 05:19:27 -0500 Subject: [PATCH] HttpClient config in Blazor IdS topic (#19157) --- .../hosted-with-identity-server.md | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/aspnetcore/blazor/security/webassembly/hosted-with-identity-server.md b/aspnetcore/blazor/security/webassembly/hosted-with-identity-server.md index e0dc0781c3..4629d02596 100644 --- a/aspnetcore/blazor/security/webassembly/hosted-with-identity-server.md +++ b/aspnetcore/blazor/security/webassembly/hosted-with-identity-server.md @@ -5,7 +5,7 @@ description: To create a new Blazor hosted app with authentication from within V monikerRange: '>= aspnetcore-3.1' ms.author: riande ms.custom: mvc -ms.date: 07/08/2020 +ms.date: 07/09/2020 no-loc: [Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR] uid: blazor/security/webassembly/hosted-with-identity-server --- @@ -135,7 +135,7 @@ To gain full control of the database schema, inherit from one of the available I In the `OidcConfigurationController` (`Controllers/OidcConfigurationController.cs`), the client endpoint is provisioned to serve OIDC parameters. -### App settings files +### App settings In the app settings file (`appsettings.json`) at the project root, the `IdentityServer` section describes the list of configured clients. In the following example, there's a single client. The client name corresponds to the app name and is mapped by convention to the OAuth `ClientId` parameter. The profile indicates the app type being configured. The profile is used internally to drive conventions that simplify the configuration process for the server. @@ -165,6 +165,22 @@ If adding authentication to an app, manually add the package to the app's projec Version="3.2.0" /> ``` +### `HttpClient` configuration + +In `Program.Main` (`Program.cs`), a named (`HostIS.ServerAPI`) is configured to supply instances that include access tokens when making requests to the server API: + +```csharp +builder.Services.AddHttpClient("HostIS.ServerAPI", + client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)) + .AddHttpMessageHandler(); + +builder.Services.AddTransient(sp => sp.GetRequiredService() + .CreateClient("HostIS.ServerAPI")); +``` + +> [!NOTE] +> If you're configuring a Blazor WebAssembly app to use an existing Identity Server instance that isn't part of a Blazor Hosted solution, change the base address registration from (`builder.HostEnvironment.BaseAddress`) to the server app's API authorization endpoint URL. + ### API authorization support The support for authenticating users is plugged into the service container by the extension method provided inside the [`Microsoft.AspNetCore.Components.WebAssembly.Authentication`](https://www.nuget.org/packages/Microsoft.AspNetCore.Components.WebAssembly.Authentication/) package. This method sets up the services required by the app to interact with the existing authorization system.