Skip to content

Commit a397309

Browse files
authored
[Blazor] additional-scenarios.md - await async where possible (#35560)
1 parent 2e1aab3 commit a397309

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

aspnetcore/blazor/security/additional-scenarios.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,17 @@ public class TokenHandler(IHttpContextAccessor httpContextAccessor) :
121121
protected override async Task<HttpResponseMessage> SendAsync(
122122
HttpRequestMessage request, CancellationToken cancellationToken)
123123
{
124-
var accessToken = httpContextAccessor.HttpContext?
125-
.GetTokenAsync("access_token").Result ??
124+
if (httpContextAccessor.HttpContext is null)
125+
{
126+
throw new Exception("HttpContext.Current not available.");
127+
}
128+
129+
var accessToken = await httpContextAccessor.HttpContext.GetTokenAsync("access_token");
130+
131+
if (accessToken is null)
132+
{
126133
throw new Exception("No access token");
134+
}
127135

128136
request.Headers.Authorization =
129137
new AuthenticationHeaderValue("Bearer", accessToken);
@@ -868,7 +876,14 @@ public class AuthenticationStateHandler(
868876
{
869877
var authStateProvider = circuitServicesAccessor.Services?
870878
.GetRequiredService<AuthenticationStateProvider>();
871-
var authState = authStateProvider?.GetAuthenticationStateAsync().Result;
879+
880+
if (authStateProvider is null)
881+
{
882+
throw new Exception("AuthenticationStateProvider not available.");
883+
}
884+
885+
var authState = await authStateProvider.GetAuthenticationStateAsync();
886+
872887
var user = authState?.User;
873888

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

0 commit comments

Comments
 (0)