-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathNtlmHttpClientFactory.cs
More file actions
62 lines (50 loc) · 2.25 KB
/
NtlmHttpClientFactory.cs
File metadata and controls
62 lines (50 loc) · 2.25 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Net;
using System.Net.Http;
using System.Security.Authentication;
namespace SharpHoundCommonLib.Ntlm;
public interface INtlmHttpClientFactory {
HttpClient CreateUnauthenticatedClient();
HttpClient CreateAuthenticatedHttpClient(Uri Url, string authPackage = "Kerberos");
}
public class NtlmHttpClientFactory : INtlmHttpClientFactory {
private readonly SslProtocols _sslProtocols;
/// <summary>
/// Creates an HttpClientFactory whose handlers will negotiate TLS using OS/framework defaults.
/// </summary>
public NtlmHttpClientFactory() : this(SslProtocols.None) { }
/// <summary>
/// Creates an HttpClientFactory whose handlers will restrict TLS negotiation to the specified protocols.
/// Use this overload when a specific set of legacy protocols must be supported for a target service,
/// rather than setting <see cref="System.Net.ServicePointManager.SecurityProtocol"/> process-wide.
/// </summary>
/// <param name="sslProtocols">
/// The SSL/TLS protocols to allow. Pass <see cref="SslProtocols.None"/> to defer to OS/framework defaults.
/// </param>
public NtlmHttpClientFactory(SslProtocols sslProtocols) {
_sslProtocols = sslProtocols;
}
public HttpClient CreateUnauthenticatedClient() {
var handler = new HttpClientHandler {
ServerCertificateCustomValidationCallback =
(httpRequestMessage, cert, cetChain, policyErrors) => true,
UseDefaultCredentials = false
};
if (_sslProtocols != SslProtocols.None)
handler.SslProtocols = _sslProtocols;
return new HttpClient(handler);
}
public HttpClient CreateAuthenticatedHttpClient(Uri Url, string authPackage = "Kerberos") {
var handler = new HttpClientHandler {
Credentials = new CredentialCache() {
{ Url, authPackage, CredentialCache.DefaultNetworkCredentials }
},
PreAuthenticate = true,
ServerCertificateCustomValidationCallback =
(httpRequestMessage, cert, cetChain, policyErrors) => true,
};
if (_sslProtocols != SslProtocols.None)
handler.SslProtocols = _sslProtocols;
return new HttpClient(handler);
}
}