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: docs/06-authentication-and-authorization.md
+86-97Lines changed: 86 additions & 97 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -210,136 +210,125 @@ The user is logged in and redirected back to the home page.
210
210
211
211
## Request an access token
212
212
213
-
Even though you are now logged in, placing an order still fails because the HTTP request to place the order requires a valid access token. To request an access token use the `IAccessTokenProvider` service. If requesting an access token succeeds, add it to the request with a standard Authentication header with scheme Bearer. If the token request fails, use the `NavigationManager` service to redirect the user to the authorization service to request a new token.
213
+
Even though you are now logged in, placing an order still fails because the HTTP request to place the order requires a valid access token. To request access tokens and attach them to outbound requests, use the `BaseAddressAuthorizationMessageHandler` with the `HttpClient` that you're using to make the request. This message handler will acquire access tokens using the built-in `IAccessTokenProvider` service and attach them to each request using the standard Authorization header. If an access token cannot be acquired, an `AccessTokenNotAvailableException` is thrown, which can be used to redirect the user to the login page to authorize a new token.
214
214
215
-
*BlazingPizza.Client/Pages/Checkout.razor*
215
+
To add the `BaseAddressAuthorizationMessageHandler` to our `HttpClient` in our app, we'll use the [IHttpClientFactory` helpers from ASP.NET Core](https://docs.microsoft.com/aspnet/core/fundamentals/http-requests) with a strongly typed client.
216
216
217
-
```razor
218
-
@page "/checkout"
219
-
@attribute [Authorize]
220
-
@inject OrderState OrderState
221
-
@inject HttpClient HttpClient
222
-
@inject NavigationManager NavigationManager
223
-
@inject IAccessTokenProvider TokenProvider
217
+
To create the strongly typed client, add a new `OrdersClient` class to the client project. The class should take an `HttpClient` in its constructor, and provide methods getting and placing orders:
224
218
225
-
<div class="main">
226
-
...
227
-
</div>
219
+
*BlazingPizza.Client/OrdersClient.cs*
228
220
229
-
@code {
230
-
bool isSubmitting;
231
-
232
-
async Task PlaceOrder()
221
+
```csharp
222
+
usingSystem;
223
+
usingSystem.Collections.Generic;
224
+
usingSystem.Linq;
225
+
usingSystem.Net.Http;
226
+
usingSystem.Net.Http.Json;
227
+
usingSystem.Threading.Tasks;
228
+
229
+
namespaceBlazingPizza.Client
230
+
{
231
+
publicclassOrdersClient
233
232
{
234
-
isSubmitting = true;
233
+
privatereadonlyHttpClienthttpClient;
235
234
236
-
var tokenResult = await TokenProvider.RequestAccessToken();
237
-
if (tokenResult.TryGetToken(out var accessToken))
235
+
publicOrdersClient(HttpClienthttpClient)
238
236
{
239
-
var request = new HttpRequestMessage(HttpMethod.Post, "orders");
Update the `MyOrders` and `OrderDetails` components to also make authenticated HTTP requests.
259
+
Register the `OrdersClient` as a typed client, with the underlying `HttpClient` configured with the correct base address and the `BaseAddressAuthorizationMessageHandler`.
Update each page where an `HttpClient` is used to manage orders to use the new typed `OrdersClient`. Inject an `OrdersClient` instead of an `HttpClient` and use the new client to make the API call. Wrap each call in a `try-catch` that handles exceptions of type `AccessTokenNotAvailableException` by calling the provided `Redirect()` method.
0 commit comments