2017-10-25 2 views
1

Ich möchte alle Blob-Container aus azurblauen Speicher mit HTTP-Client-Anfrage erhalten. Ich habe eine Probe gemacht, aber ich bin mit dem verbotenen Fehler 403 konfrontiert.Azure Blob Holen Sie sich Speicherplatz Holen Sie sich alle Blobs mit HTTP-Anfrage mit Blick auf 403 verbotener Fehler

ich meinen Code, angebracht

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

Antwort

1

Es gibt einige Probleme mit Ihrer AuthorizationHeader1 Methode. Anweisungen zum Erstellen des Autorisierungsheaders finden Sie unter Authentication for the Azure Storage Services. Bitte versuchen Sie diesen Code:

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

ja du bist jetzt richtig, ich habe es geändert. Vielen Dank. –