Simplest c# sample to call azure storage api

Finally called the azure storage api, stucked for one hour in library. There is a / in + “/{0}/\ncomp:list”, Account);
Remember two things:
1. If you are calling to azure management api, you need a cert. Read https://docs.microsoft.com/en-us/azure/cloud-services/cloud-services-certs-create and https://docs.microsoft.com/en-us/azure/cloud-services/cloud-services-configure-ssl-certificate-portal
2. If you are calling azure storage api, you just need a key, no cert if needed.
using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; namespace AzureStorageRestFulAPI { class Program { static string subscriptionID = @"f6e7edac-3976-4289-8dc2-4235819a5725173"; static string Key = @"sGQyidifHtuV6aLLvc7pqs1231231vPO6UohIEl2BCQWdFLBID91gKNwxMjbmLtsE4DnpxZs6QKNQl//vPEFrg=="; static string Account = @"quantr1"; static void Main(string[] args) { listContainers(); //createNewStorageAccount("My new storage account from c#"); Console.Read(); } private static String SignThis(string StringToSign, string Key, string Account) { String signature = string.Empty; byte[] unicodeKey = Convert.FromBase64String(Key); using (HMACSHA256 hmacSha256 = new HMACSHA256(unicodeKey)) { Byte[] dataToHmac = System.Text.Encoding.UTF8.GetBytes(StringToSign); signature = Convert.ToBase64String(hmacSha256.ComputeHash(dataToHmac)); } String authorizationHeader = String.Format( System.Globalization.CultureInfo.InvariantCulture, "{0} {1}:{2}", "SharedKey", Account, signature); Console.WriteLine("\n authorizationHeader " + authorizationHeader); return authorizationHeader; } static string formatXml(string xml) { try { XDocument doc = XDocument.Parse(xml); return doc.ToString(); } catch (Exception) { // Handle and throw if fatal exception here; don't just ignore them return xml; } } static void listContainers() { DateTime dt = DateTime.UtcNow; string StringToSign = String.Format("GET\n" + "\n" // content encoding + "\n" // content language + "\n" // content length + "\n" // content md5 + "\n" // content type + "\n" // date + "\n" // if modified since + "\n" // if match + "\n" // if none match + "\n" // if unmodified since + "\n" // range + "x-ms-date:" + dt.ToString("R") + "\n" // headers + "x-ms-version:2014-02-14\n" + "/{0}/\ncomp:list", Account); Console.WriteLine("\n StringToSign " + StringToSign); string auth = SignThis(StringToSign, Key, Account); string method = "GET"; string urlPath = string.Format("https://{0}.blob.core.windows.net/?comp=list", Account); Uri uri = new Uri(urlPath); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.Method = method; request.Headers.Add("Authorization", auth); request.Headers.Add("x-ms-date", dt.ToString("R")); request.Headers.Add("x-ms-version", "2014-02-14"); Console.WriteLine("auth=" + auth); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { string text = reader.ReadToEnd(); Console.WriteLine("\n" + formatXml(text)); } } } static void createNewStorageAccount(string storageAccount) { DateTime dt = DateTime.UtcNow; string method = "POST"; string urlPath = string.Format("https://management.core.windows.net/{0}/services/storageservices", subscriptionID); Console.WriteLine("urlPath=" + urlPath); Uri uri = new Uri(urlPath); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.Method = method; request.Headers.Add("x-ms-date", dt.ToString("R")); request.Headers.Add("x-ms-version", "2014-02-14"); var postData = @"<?xml version=""1.0"" encoding=""utf-8""?> <CreateStorageServiceInput xmlns=""http://schemas.microsoft.com/windowsazure""> <ServiceName>{0}</ServiceName> <Description>Testing description</Description> <Label> Testing Label </Label> <Location>Southeast Asia</Location> </CreateStorageServiceInput>"; var data = Encoding.ASCII.GetBytes(String.Format(postData, "fuckstorageaccount")); request.ContentType = "application/xml"; request.ContentLength = data.Length; //Console.WriteLine("auth=" + auth); using (var stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { string text = reader.ReadToEnd(); Console.WriteLine("\n" + formatXml(text)); } } } } }
One Response
学无止境,认真拜读!