Quantcast
Channel: PDCM's Activities
Viewing all articles
Browse latest Browse all 170

403 Forbidden: Azure Blob Storage

$
0
0

Hi All,

I am trying to retrieve Blob from Azure Blob Storage. I'm using the code below to connect, but I'm getting 403: Forbidden. Any idea how I can fix this? Thank you

public static void UsingGet()
        {
            string storageKey = "<StorageKey>";
            string storageAccount = "<Storageaccountname>";
            string containerName = "<containername>";
            string blobName = "<filename>";

            string method = "GET";
            string requestUri = $"https://{storageAccount}.blob.core.windows.net/{containerName}/{blobName}";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);

            request.Method = method;
            request.ContentLength = 0;

            string now = DateTime.UtcNow.ToString("R");

            request.Headers.Add("x-ms-version", "2018-03-28");
            request.Headers.Add("x-ms-date", now);
            request.Headers.Add("x-ms-blob-type", "BlockBlob");
            request.Headers.Add("Authorization", AuthorizationHeader(method, now, request, storageAccount, storageKey, containerName, blobName));
            Console.WriteLine(request.Headers.ToString());
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            
        }

public static string AuthorizationHeader(string method, string now, HttpWebRequest request, string storageAccount, string storageKey, string containerName, string blobName)
        {
            string headerResource = $"x-ms-blob-type:BlockBlob\nx-ms-date:{now}\nx-ms-version:2018-03-28";
            string urlResource = $"https://{storageAccount}.blob.core.windows.net/{containerName}/{blobName}";
            string stringToSign = $"{method}\n\n\n{request.ContentLength}\n\n{request.ContentType}\n\n\n\n\n\n\n{headerResource}\n{urlResource}";

            HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(storageKey));
            string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));

            String AuthorizationHeader = String.Format("{0} {1}:{2}", "SharedKey", storageAccount, signature);
            return AuthorizationHeader;
        }



Viewing all articles
Browse latest Browse all 170

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>