Created
July 12, 2014 03:42
-
-
Save JerryBian/d4368a980870b7c286d4 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Text; | |
namespace ConsoleApplication1 | |
{ | |
using System.Globalization; | |
using System.Net.Http; | |
using System.Security.Cryptography; | |
class Program | |
{ | |
private const string StorageAccount = ""; | |
private const string StorageAccountKey = ""; | |
private const string QueueName = ""; | |
private const string QueueEndPoint = ""; | |
static void Main(string[] args) | |
{ | |
CreateQueue(); | |
Console.ReadKey(); | |
} | |
static string GetAuthorizationHeader(string canonicalizedString) | |
{ | |
var signature = string.Empty; | |
using (var hash = new HMACSHA256(Convert.FromBase64String(StorageAccountKey))) | |
{ | |
var data = Encoding.UTF8.GetBytes(canonicalizedString); | |
signature = Convert.ToBase64String(hash.ComputeHash(data)); | |
} | |
return string.Format(CultureInfo.InvariantCulture, "{0} {1}:{2}", "SharedKey", StorageAccount, signature); | |
} | |
static void CreateQueue() | |
{ | |
var requestMethod = "PUT"; | |
var storageServiceVersion = "2014-02-14"; | |
var date = DateTime.UtcNow.ToString("R"); | |
var canonicalizedHeaders = string.Format("x-ms-date:{0}\nx-ms-version:{1}", date, storageServiceVersion); | |
var canonicalizedResource = string.Format("/{0}/{1}", StorageAccount, QueueName); | |
var stringToSign = string.Format( | |
"{0}\n\n\n{1}\n\n\n\n\n\n\n\n\n{2}\n{3}", | |
requestMethod, | |
0, | |
canonicalizedHeaders, | |
canonicalizedResource); | |
var authorizationHeader = GetAuthorizationHeader(stringToSign); | |
using (var httpClient = new HttpClient()) | |
{ | |
httpClient.BaseAddress = new Uri(QueueEndPoint); | |
httpClient.DefaultRequestHeaders.Clear(); | |
httpClient.DefaultRequestHeaders.Add("x-ms-date", date); | |
httpClient.DefaultRequestHeaders.Add("x-ms-version", storageServiceVersion); | |
httpClient.DefaultRequestHeaders.Add("Authorization", authorizationHeader); | |
var result = httpClient.PutAsync(QueueName, new StringContent(string.Empty)); | |
result.Wait(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment