2017-08-22 27 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