@@ -34,19 +34,21 @@ using Microsoft.AspNetCore.Authentication;
3434
3535public 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
180190using 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}" );
182192using var response = await client .SendAsync (request );
183193```
184194
185195Example:
186196
187197``` csharp
188198using var request = new HttpRequestMessage (HttpMethod .Get , " /weather-forecast" );
189- using var client = ClientFactory .CreateClient (" ExternalApi" );
199+ var client = ClientFactory .CreateClient (" ExternalApi" );
190200using 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 )
0 commit comments