Skip to content

Instantly share code, notes, and snippets.

@blowdart
Created November 7, 2012 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blowdart/4035399 to your computer and use it in GitHub Desktop.
Save blowdart/4035399 to your computer and use it in GitHub Desktop.
Hello request in flight body
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
if (request.Headers.Date == null)
{
request.Headers.Date = DateTime.UtcNow;
}
// Check if we have request content, if we do then we need to add a Content-MD5 header.
if (request.Content != null && request.Content.Headers.ContentMD5 == null)
{
await request.Content.LoadIntoBufferAsync().ConfigureAwait(false);
using (var bodyStream = new MemoryStream())
{
await request.Content.CopyToAsync(bodyStream).ConfigureAwait(false);
bodyStream.Position = 0;
using (var md5 = new MD5CryptoServiceProvider())
{
request.Content.Headers.ContentMD5 = md5.ComputeHash(bodyStream);
}
}
}
byte[] hash = SharedKeySignature.Calculate(request, this.accountName, this.sharedSecret);
request.Headers.Authorization = new AuthenticationHeaderValue(
SharedKeyAuthentication.Scheme,
string.Format(CultureInfo.InvariantCulture, "{0}:{1}", this.accountName, Convert.ToBase64String(hash)));
// And finally hand off the request to the InnerHandler.
return await base.SendAsync(request, cancellationToken);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment