Last active
October 30, 2015 09:11
-
-
Save sshibani/4d97814cd81686e80101 to your computer and use it in GitHub Desktop.
Add Basic authentication header to DD4T Rest Provider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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