web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

403 Forbidden: Azure Blob Storage

(0) ShareShare
ReportReport
Posted on by 325

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;
        }


*This post is locked for comments

I have the same question (0)
  • PeteMc Profile Picture
    55 on at

    If you connect to the storage account using azure storage explorer you will be able to see a $logs blob storage, there might be more detail in there as to why you are getting the 403 error. It is also a good check to see if you are hitting the right storage account.

  • Rizwan Aarif Profile Picture
    480 on at

    I am also  getting the same error. Did you get it solved PDCM?

  • Suggested answer
    PDCM Profile Picture
    325 on at

    Hi Rizwan,

    I solved this by comparing the Authorization Header of a working authentication code and my code, specifically on canonicalized header and resource.

    This is the link for the working authentication

    docs.microsoft.com/.../storage-rest-api-auth

    Let me know if you are having issues with the application. I'm happy to help.

  • Rizwan Aarif Profile Picture
    480 on at

    Hi PDCM,

    Thanks a lot, I solved  it using the  same way yesterday. I had a working code but an older x-ms-version but that worked for me.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans