Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit 184ed15

Browse files
davidfowlReubenBond
authored andcommitted
Clean up the auth delegating handler
1 parent af42aab commit 184ed15

File tree

2 files changed

+7
-20
lines changed

2 files changed

+7
-20
lines changed

src/Web/WebMVC/Extensions/Extensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ public override async ValueTask TransformRequestAsync(HttpContext httpContext, H
102102
// Set the access token as a bearer token for the outgoing request
103103
var accessToken = await httpContext.GetTokenAsync("access_token");
104104

105-
proxyRequest.Headers.Authorization = new("Bearer", accessToken);
105+
if (accessToken is not null)
106+
{
107+
proxyRequest.Headers.Authorization = new("Bearer", accessToken);
108+
}
106109

107110
await base.TransformRequestAsync(httpContext, proxyRequest, destinationPrefix, cancellationToken);
108111
}

src/Web/WebMVC/Infrastructure/HttpClientAuthorizationDelegatingHandler.cs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,13 @@ public HttpClientAuthorizationDelegatingHandler(IHttpContextAccessor httpContext
1212

1313
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
1414
{
15-
var authorizationHeader = _httpContextAccessor.HttpContext
16-
.Request.Headers["Authorization"];
15+
var accessToken = await _httpContextAccessor.HttpContext.GetTokenAsync("access_token");
1716

18-
if (!string.IsNullOrEmpty(authorizationHeader))
17+
if (accessToken is not null)
1918
{
20-
request.Headers.Add("Authorization", new List<string>() { authorizationHeader });
21-
}
22-
23-
var token = await GetToken();
24-
25-
if (token != null)
26-
{
27-
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
19+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
2820
}
2921

3022
return await base.SendAsync(request, cancellationToken);
3123
}
32-
33-
async Task<string> GetToken()
34-
{
35-
const string ACCESS_TOKEN = "access_token";
36-
37-
return await _httpContextAccessor.HttpContext
38-
.GetTokenAsync(ACCESS_TOKEN);
39-
}
4024
}

0 commit comments

Comments
 (0)