2017-08-22 108 views
0

我的主要目標是爲文件創建一個SAS網址(沒有天青庫)。 我試圖用blob創建蔚藍存儲SAS,並且一切正常。當我嘗試在File中做同樣的事情時,我得到一個錯誤。這是我的代碼:如何創建藍色文件存儲SAS網址(沒有天藍色的庫)?

string azAccName = "AccountName"; 
string resource = "/upgfile/prt.png"; 
string endPoint = "https://" + azAccName + ".file.core.windows.net"; 
string uri = endPoint + resource; 
string _now = DateTime.UtcNow.ToString("s") + "Z";  
string _noww = DateTime.UtcNow.AddHours(3).AddMinutes(5).ToString("s") + "Z"; 
string StorageKey = "xxx"; 

string signedpermissions = "r"; 
string signedstart = _now;//"2017-02-14"; //yyyy-mm--dd 
string signedexpiry = _noww;// "2017-02-14"; 
string canonicalizedresource = "/file/" + azAccName + resource; //"/blob/myaccount/music/intro.mp3" 
string signedidentifier = ""; //YWJjZGVmZw== 
string signedIP = ""; 
string signedProtocol = "https"; 
string signedversion = "2015-02-21"; 
string rscc = ""; //Cache-Control 
string rscd = "file; attachment"; //Content-Disposition    
string rsce = ""; //Content-Encoding 
string rscl = ""; //Content-Language 
string rsct = "binary"; //Content-Type  binary 


string StringToSign = signedpermissions + "\n" + 
       signedstart + "\n" + 
       signedexpiry + "\n" + 
       canonicalizedresource + "\n" + 
       signedidentifier + "\n" + 
       signedversion + "\n" + 
       rscc + "\n" + 
       rscd + "\n" + 
       rsce + "\n" + 
       rscl + "\n" + 
       rsct; 

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

string link = String.Format("{0}?sv={1}&st={2}&se={3}&sr={4}&sp={5}&rscd={8}&rsct={9}&spr={6}&sig={7}", 
             uri, 
             signedversion, 
             signedstart, 
             signedexpiry, 
             "c", //b for blob 
             signedpermissions, 
             "https", 
             signature.Replace("/", "%2"), 
             rscd,/////////////// 
             rsct); 

我得到這個錯誤。

<Error> 
<Code>AuthenticationFailed</Code> 
<Message> 
Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:92eda75a-001a-0072-501d-1bb6fd000000 Time:2017-08-22T08:03:58.6115733Z 
</Message> 
<AuthenticationErrorDetail>Signature fields not well formed.</AuthenticationErrorDetail> 
</Error> 

我有幾乎相同的blob代碼(小差異),它能正常工作。 有什麼建議嗎?

回答

1

我相信這個問題在stringToSign中缺少參數。您必須包括所有參數指定here

StringToSign = signedpermissions + "\n" + 
       signedstart + "\n" + 
       signedexpiry + "\n" + 
       canonicalizedresource + "\n" + 
       signedidentifier + "\n" + 
       signedIP + "\n" + 
       signedProtocol + "\n" + 
       signedversion + "\n" + 
       rscc + "\n" + 
       rscd + "\n" + 
       rsce + "\n" + 
       rscl + "\n" + 
       rsct 

如果你不使用的參數(例如signedIP你的情況),您必須指定一個空行。

在此基礎上,你StringToSign應該是:

string StringToSign = signedpermissions + "\n" + 
       signedstart + "\n" + 
       signedexpiry + "\n" + 
       canonicalizedresource + "\n" + 
       signedidentifier + "\n" + 
       "\n" + //For signed IP 
       "\n" + //For signed Protocol 
       signedversion + "\n" + 
       rscc + "\n" + 
       rscd + "\n" + 
       rsce + "\n" + 
       rscl + "\n" + 
       rsct; 

此外sr(簽署資源型)在link應該是f(文件),而不是c您使用。

+0

非常感謝。我的問題是sr(f而不是c)。 – kostas