Skip to content

Commit a28b6ba

Browse files
authored
Merge to Live (#35561)
2 parents 05dfb04 + 26eec4a commit a28b6ba

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

aspnetcore/blazor/security/additional-scenarios.md

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,21 @@ using Microsoft.AspNetCore.Authentication;
3434

3535
public class AuthenticationProcessor(IHttpContextAccessor httpContextAccessor)
3636
{
37-
public string? GetAccessToken()
37+
public async Task<string?> GetAccessToken()
3838
{
39+
if (httpContextAccessor.HttpContext is null)
40+
{
41+
throw new Exception("HttpContext not available");
42+
}
43+
3944
// Approach 1: Call 'GetTokenAsync'
40-
var accessToken = httpContextAccessor.HttpContext?
41-
.GetTokenAsync("access_token").Result ??
42-
throw new Exception("No access token");
45+
var accessToken = await httpContextAccessor.HttpContext
46+
.GetTokenAsync("access_token");
4347

4448
// Approach 2: Authenticate the user and call 'GetTokenValue'
4549
/*
46-
var authResult = httpContextAccessor.HttpContext?
47-
.AuthenticateAsync().Result;
48-
var accessToken = authResult?.Properties?
49-
.GetTokenValue("access_token");
50+
var authResult = await httpContextAccessor.HttpContext.AuthenticateAsync();
51+
var accessToken = authResult?.Properties?.GetTokenValue("access_token");
5052
*/
5153

5254
return accessToken;
@@ -121,9 +123,17 @@ public class TokenHandler(IHttpContextAccessor httpContextAccessor) :
121123
protected override async Task<HttpResponseMessage> SendAsync(
122124
HttpRequestMessage request, CancellationToken cancellationToken)
123125
{
124-
var accessToken = httpContextAccessor.HttpContext?
125-
.GetTokenAsync("access_token").Result ??
126+
if (httpContextAccessor.HttpContext is null)
127+
{
128+
throw new Exception("HttpContext not available");
129+
}
130+
131+
var accessToken = await httpContextAccessor.HttpContext.GetTokenAsync("access_token");
132+
133+
if (accessToken is null)
134+
{
126135
throw new Exception("No access token");
136+
}
127137

128138
request.Headers.Authorization =
129139
new AuthenticationHeaderValue("Bearer", accessToken);
@@ -178,15 +188,15 @@ At this point, an <xref:System.Net.Http.HttpClient> created by a component can m
178188

179189
```csharp
180190
using var request = new HttpRequestMessage(HttpMethod.Get, "{REQUEST URI}");
181-
using var client = ClientFactory.CreateClient("{HTTP CLIENT NAME}");
191+
var client = ClientFactory.CreateClient("{HTTP CLIENT NAME}");
182192
using var response = await client.SendAsync(request);
183193
```
184194

185195
Example:
186196

187197
```csharp
188198
using var request = new HttpRequestMessage(HttpMethod.Get, "/weather-forecast");
189-
using var client = ClientFactory.CreateClient("ExternalApi");
199+
var client = ClientFactory.CreateClient("ExternalApi");
190200
using var response = await client.SendAsync(request);
191201
```
192202

@@ -868,7 +878,14 @@ public class AuthenticationStateHandler(
868878
{
869879
var authStateProvider = circuitServicesAccessor.Services?
870880
.GetRequiredService<AuthenticationStateProvider>();
871-
var authState = authStateProvider?.GetAuthenticationStateAsync().Result;
881+
882+
if (authStateProvider is null)
883+
{
884+
throw new Exception("AuthenticationStateProvider not available");
885+
}
886+
887+
var authState = await authStateProvider.GetAuthenticationStateAsync();
888+
872889
var user = authState?.User;
873890

874891
if (user?.Identity is not null && user.Identity.IsAuthenticated)

aspnetcore/blazor/security/webassembly/additional-scenarios.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ The configured <xref:System.Net.Http.HttpClient> is used to make authorized requ
349349
{
350350
try
351351
{
352-
using var client = ClientFactory.CreateClient("WebAPI");
352+
var client = ClientFactory.CreateClient("WebAPI");
353353

354354
var examples =
355355
await client.GetFromJsonAsync<ExampleType[]>("ExampleAPIMethod");
@@ -551,7 +551,7 @@ A component creates the <xref:System.Net.Http.HttpClient> from the <xref:System.
551551
@code {
552552
protected override async Task OnInitializedAsync()
553553
{
554-
using var client = ClientFactory.CreateClient("WebAPI.NoAuthenticationClient");
554+
var client = ClientFactory.CreateClient("WebAPI.NoAuthenticationClient");
555555

556556
var examples = await client.GetFromJsonAsync<ExampleType[]>(
557557
"ExampleNoAuthentication");

0 commit comments

Comments
 (0)