-
Notifications
You must be signed in to change notification settings - Fork 386
/
Copy pathBasicAuthHandler.cs
43 lines (37 loc) · 1.29 KB
/
BasicAuthHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Docker.DotNet.BasicAuth
{
internal class BasicAuthHandler : DelegatingHandler
{
private readonly MaybeSecureString _username;
private readonly MaybeSecureString _password;
public BasicAuthHandler(MaybeSecureString username, MaybeSecureString password, HttpMessageHandler innerHandler) : base(innerHandler)
{
_username = username.Copy();
_password = password.Copy();
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", BuildParameters());
return base.SendAsync(request, cancellationToken);
}
private string BuildParameters()
{
var authInfo = $"{_username}:{_password}";
return Convert.ToBase64String(Encoding.UTF8.GetBytes(authInfo));
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_username.Dispose();
_password.Dispose();
}
}
}
}