-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto attach authorization header by implementing AuthenticatedDelegat…
…ingHandler; Use IHttpClientFactory to create HttpClient in AuthenticatedServiceBase;
- Loading branch information
Leon Liu
committed
Oct 11, 2021
1 parent
be5b724
commit 2d31b63
Showing
11 changed files
with
91 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 0 additions & 46 deletions
46
HackSystem.Web.Authentication.Abstractions/WebServices/AuthenticatedHttpClient.cs
This file was deleted.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
HackSystem.Web.Authentication.Abstractions/WebServices/AuthenticatedHttpMessageHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Net.Http.Headers; | ||
using HackSystem.Web.Authentication.Options; | ||
using HackSystem.Web.Authentication.TokenHandlers; | ||
|
||
namespace HackSystem.Web.Authentication.WebServices; | ||
|
||
public class AuthenticatedDelegatingHandler : DelegatingHandler | ||
{ | ||
private readonly IHackSystemAuthenticationTokenHandler authenticationTokenHandler; | ||
private readonly HackSystemAuthenticationOptions options; | ||
|
||
public AuthenticatedDelegatingHandler( | ||
IHackSystemAuthenticationTokenHandler authenticationTokenHandler, | ||
IOptionsSnapshot<HackSystemAuthenticationOptions> optionsSnapshot) | ||
{ | ||
this.authenticationTokenHandler = authenticationTokenHandler; | ||
this.options = optionsSnapshot.Value; | ||
} | ||
|
||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
await this.AttachAuthorizationHeader(request); | ||
return await base.SendAsync(request, cancellationToken); | ||
} | ||
|
||
protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
this.AttachAuthorizationHeader(request).ConfigureAwait(false); | ||
return base.Send(request, cancellationToken); | ||
} | ||
|
||
protected async Task AttachAuthorizationHeader(HttpRequestMessage request) | ||
{ | ||
if (request.Headers.Authorization is null) | ||
{ | ||
var token = await this.authenticationTokenHandler.GetTokenAsync(); | ||
request.Headers.Authorization = new AuthenticationHeaderValue(this.options.AuthenticationScheme, token); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters