Skip to content

Commit b79aa64

Browse files
committed
Document how to use a custom authentication header e.g Kerberos (#3518)
* Document how to subclass the HttpConnection to provide authentication headers, e.g for Kerberos * move comment about examples to the top (cherry picked from commit 980a5df)
1 parent 0681d95 commit b79aa64

File tree

1 file changed

+59
-7
lines changed

1 file changed

+59
-7
lines changed

src/Tests/Tests/ClientConcepts/Connection/ModifyingDefaultConnection.doc.cs

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
using Tests.Core.Extensions;
1414
using Tests.Domain;
1515
using Tests.Framework;
16+
#if DOTNETCORE
17+
using System.Net.Http;
18+
using System.Net.Http.Headers;
19+
#endif
1620

1721
namespace Tests.ClientConcepts.Connection
1822
{
@@ -112,14 +116,60 @@ public void InMemoryConnectionOverloadedCtor()
112116
}
113117

114118
/**
115-
/**==== Customize HttpConnection
116-
*
117-
* For a lot of use cases subclassing HttpConnection is a great way to customize the http connection for your needs.
118-
* E.g if you want to authenticate with Kerberos, creating a custom HttpConnection as followed allows you to set the right HTTP headers.
119-
*
120-
*
121-
* TIP use something like https://www.nuget.org/packages/Kerberos.NET/ to fill in the actual blanks of this implementation
119+
* ==== Changing HttpConnection
120+
*
121+
* There may be a need to change how the default `HttpConnection` works, for example, to add an X509 certificate
122+
* to the request, change the maximum number of connections allowed to an endpoint, etc.
123+
*
124+
* By deriving from `HttpConnection`, it is possible to change the behaviour of the connection. The following
125+
* provides some examples
126+
*
127+
*
128+
* [[servicepoint-behaviour]]
129+
* ===== ServicePoint behaviour
130+
*
131+
* If you are running on the Desktop CLR you can override specific properties for the current `ServicePoint` easily
132+
* by overriding `AlterServicePoint` on an `IConnection` implementation deriving from `HttpConnection`
133+
*/
134+
#if !DOTNETCORE
135+
public class MyCustomHttpConnection : HttpConnection
136+
{
137+
protected override HttpRequestMessage CreateRequestMessage(RequestData requestData)
138+
{
139+
var message = base.CreateRequestMessage(requestData);
140+
var header = string.Empty;
141+
message.Headers.Authorization = new AuthenticationHeaderValue("Negotiate", header);
142+
return message;
143+
}
144+
}
145+
146+
147+
/**
148+
* As before, a new instance of the custom connection is passed to `ConnectionSettings` in order to
149+
* use
150+
*/
151+
public void UseX509CertificateHttpConnection()
152+
{
153+
var connection = new X509CertificateHttpConnection();
154+
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
155+
var settings = new ConnectionSettings(connectionPool, connection);
156+
var client = new ElasticClient(settings);
157+
}
158+
/**
159+
* See <<working-with-certificates, Working with certificates>> for further details.
122160
*/
161+
#endif
162+
#if DOTNETCORE
163+
/*
164+
* [[kerberos-authentication]]
165+
* ===== Kerberos Authentication
166+
*
167+
* For a lot of use cases subclassing HttpConnection is a great way to customize the http connection for your needs.
168+
* E.g if you want to authenticate with Kerberos, creating a custom HttpConnection as followed allows you to set the right HTTP headers.
169+
*
170+
*
171+
* TIP use something like https://www.nuget.org/packages/Kerberos.NET/ to fill in the actual blanks of this implementation
172+
*/
123173
public class KerberosConnection : HttpConnection
124174
{
125175
protected override HttpRequestMessage CreateRequestMessage(RequestData requestData)
@@ -130,6 +180,8 @@ protected override HttpRequestMessage CreateRequestMessage(RequestData requestDa
130180
return message;
131181
}
132182
}
183+
#endif
184+
133185

134186

135187
}

0 commit comments

Comments
 (0)