Skip to content

Instantly share code, notes, and snippets.

@sshibani
Last active October 30, 2015 09:11
Show Gist options
  • Save sshibani/4d97814cd81686e80101 to your computer and use it in GitHub Desktop.
Save sshibani/4d97814cd81686e80101 to your computer and use it in GitHub Desktop.
Add Basic authentication header to DD4T Rest Provider
public class HttpClientAuthentication : DD4T.Providers.Rest.IHttpMessageHandlerFactory
{
public HttpMessageHandler CreatePipeline(HttpMessageHandler innerHandler)
{
return HttpClientFactory.CreatePipeline(innerHandler, new[] {
new BasicAuthenticationHandler()
});
}
}
public class BasicAuthenticationHandler : DelegatingHandler
{
protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
System.Threading.CancellationToken cancellationToken)
{
var credentials = string.Format("{0}:{1}", "myusername", "mypassword");
var byteArray = System.Text.Encoding.ASCII.GetBytes(credentials);
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
return base.SendAsync(request, cancellationToken);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment