I am trying to set CORS properties on my Windows Azure blazs storage account. I am using an ASP.NET server to send a PUT request.
The server sends a "Deny" response saying "The server was unable to authenticate the request. Verify that the authorization header value is configured correctly, including the signature."
So that should be something in my authentication header. Here are two functions that I use to get the title.
public string GetWindowsAzureAuthenticationHeader(string verb)
{
string stringToSign = String.Format("{0}\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "x-ms-date:" + DateTime.UtcNow.ToString("R") + "\nx-ms-version:2013-08-15\n"
+ "/{1}\ncomp:properties\nrestype:service", verb, CloudConfig.StorageAccountName);
return SignThis(stringToSign, CloudConfig.StorageAccountKey, CloudConfig.StorageAccountName);
}
private string SignThis(string stringToSign, string key, string account)
{
string signature;
var unicodeKey = Convert.FromBase64String(key);
using (var hmacSha256 = new HMACSHA256(unicodeKey))
{
var dataToHmac = Encoding.UTF8.GetBytes(stringToSign);
signature = Convert.ToBase64String(hmacSha256.ComputeHash(dataToHmac));
}
String authorizationHeader = String.Format(
CultureInfo.InvariantCulture,
"{0} {1}:{2}",
"SharedKey",
account,
signature);
return authorizationHeader;
}
This is a controller action that sends a request. The _mediaFactory.GetWindowsAzureCors method returns the contents of the XML file with my CORS request.
var content = Encoding.UTF8.GetBytes(_mediaFactory.GetWindowsAzureCors(ControllerContext.HttpContext.Server));
var request = (HttpWebRequest)WebRequest.Create(CloudConfig.StorageAccountUri);
request.Method = "PUT";
request.Headers.Add("x-ms-date", DateTime.UtcNow.ToString("R"));
request.Headers.Add("x-ms-version", "2013-08-15");
request.ContentType = "text/plain; charset=UTF-8";
request.Host = string.Format("{0}.blob.core.windows.net", CloudConfig.StorageAccountName);
request.Headers.Add("Authorization", _mediaFactory.GetWindowsAzureAuthenticationHeader(request.Method));
request.GetRequestStream().Write(content, 0, content.Length);
using (var response = (HttpWebResponse) request.GetResponse())
{
model.StatusCode = response.StatusCode;
model.Response = response.StatusDescription;
}
What am I doing wrong?
source
share