Azure Blob获取存储使用http请求获取所有Blob面临403禁止错误

Azure Blob获取存储使用http请求获取所有Blob面临403禁止错误,azure,azure-storage,azure-storage-blobs,Azure,Azure Storage,Azure Storage Blobs,我想使用http客户端请求从azure存储获取所有blob容器。 我做了一个样本,但我面临着禁止403错误 我附上了我的密码 private const string ListofContainersURL = "https://{0}.blob.core.windows.net/?comp=list&maxresults=3"; //https://myaccount.blob.core.windows.net/?comp=list&maxresults=3

我想使用http客户端请求从azure存储获取所有blob容器。 我做了一个样本,但我面临着禁止403错误

我附上了我的密码

        private const string ListofContainersURL = "https://{0}.blob.core.windows.net/?comp=list&maxresults=3"; //https://myaccount.blob.core.windows.net/?comp=list&maxresults=3


       public  string ListofContainersinBlob()
            {

                string Requesturl = string.Format(ListofContainersURL, storageAccount );
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Requesturl);
                string now = DateTime.UtcNow.ToString("R");
                request.Method = "GET";
                request.Headers.Add("x-ms-version", "2015-12-11");
                request.Headers.Add("x-ms-date", now);
                request.Headers.Add("Authorization", AuthorizationHeader1("GET", now, request, storageAccount, storageKey));
                var response =  request.GetResponseAsync();

                using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
                {
                    return resp.StatusCode.ToString();
                }

            }


            private string AuthorizationHeader1(string method, string now, HttpWebRequest request, string storageAccount, string storageKey)
            {
               string headerResource = $"x-ms-blob-type:BlockBlob\nx-ms-date:{now}\nx-ms-version:2015-12-11";
                string urlResource = $"/{storageAccount}";
                String AuthorizationHeader = String.Format("{0} {1}:{2}", "SharedKey", storageAccount, storageKey);
                return AuthorizationHeader;
            }

您的
AuthorizationHeader1
方法存在一些问题。有关如何构造授权标头的说明,请参阅。请尝试以下代码:

    private static string AuthorizationHeader1(string method, string now, HttpWebRequest request, string storageAccount, string storageKey)
    {
        string headerResource = $"x-ms-date:{now}\nx-ms-version:2015-12-11";
        string canonicalizedResource = $"/{storageAccount}/\ncomp:list\nmaxresults:3";
        var contentEncoding = "";
        var contentLanguage = "";
        var contentLength = "";
        var contentMd5 = "";
        var contentType = "";
        var date = "";
        var ifModifiedSince = "";
        var ifMatch = "";
        var ifNoneMatch = "";
        var ifUnmodifiedSince = "";
        var range = "";
        var stringToSign = $"{method}\n{contentEncoding}\n{contentLanguage}\n{contentLength}\n{contentMd5}\n{contentType}\n{date}\n{ifModifiedSince}\n{ifMatch}\n{ifNoneMatch}\n{ifUnmodifiedSince}\n{range}\n{headerResource}\n{canonicalizedResource}";
        var signature = "";
        using (var hmacSha256 = new HMACSHA256(Convert.FromBase64String(storageKey)))
        {
            var dataToHmac = Encoding.UTF8.GetBytes(stringToSign);
            signature = Convert.ToBase64String(hmacSha256.ComputeHash(dataToHmac));
        }
        String AuthorizationHeader = String.Format("{0} {1}:{2}", "SharedKey", storageAccount, signature);
        return AuthorizationHeader;
    }

是的,你是对的,现在我改了。非常感谢你。