Skip to content

Commit 6fc513c

Browse files
authored
fix: Set auth header in basic auth handler implementation (#29)
1 parent bcf0c73 commit 6fc513c

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/Docker.DotNet.BasicAuth/BasicAuthHandler.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,27 @@ internal class BasicAuthHandler : DelegatingHandler
66

77
private readonly MaybeSecureString _password;
88

9+
private readonly Lazy<AuthenticationHeaderValue> _authHeader;
10+
911
public BasicAuthHandler(MaybeSecureString username, MaybeSecureString password, HttpMessageHandler httpMessageHandler)
1012
: base(httpMessageHandler)
1113
{
1214
_username = username.Copy();
1315
_password = password.Copy();
16+
17+
_authHeader = new Lazy<AuthenticationHeaderValue>(() =>
18+
{
19+
var credentials = $"{_username}:{_password}";
20+
var bytes = Encoding.ASCII.GetBytes(credentials);
21+
var base64 = Convert.ToBase64String(bytes);
22+
return new AuthenticationHeaderValue("Basic", base64);
23+
});
24+
}
25+
26+
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
27+
{
28+
request.Headers.Authorization = _authHeader.Value;
29+
return base.SendAsync(request, cancellationToken);
1430
}
1531

1632
protected override void Dispose(bool disposing)

0 commit comments

Comments
 (0)