为什么内容处理不能在我的Azure SAS令牌中工作?

时间:2017-09-18 15:47:37

标签: php azure token

出于某种原因,我似乎无法在未收到“签名不匹配”错误的情况下设置令牌的内容处置。

请参阅下面的代码:如果我将$rscd变量设置为空字符串但是当我将其设置为“file; attachment”时,$start = (new \DateTime())->setTimezone(new \DateTimeZone('GMT'))->modify('-1 minutes'); $end = (new \DateTime())->setTimezone(new \DateTimeZone('GMT'))->modify('+10 minutes'); $start = $start->format('Y-m-d\TH:i:s\Z'); $end = $end->format('Y-m-d\TH:i:s\Z'); $rscd = "file; attachment"; //$rscd = ""; $spr = (getenv('APP_ENV') == 'production') ? "https" : "http,https"; $toSign = $storageAccount . "\n"; $toSign .= "rwdlac" . "\n"; $toSign .= "b" . "\n"; $toSign .= "sco" . "\n"; $toSign .= $start . "\n"; $toSign .= $end . "\n"; $toSign .= $rscd . "\n"; $toSign .= $spr . "\n"; $toSign .= "2017-04-17" . "\n"; $signature = rawurlencode(base64_encode(hash_hmac('sha256', $toSign, base64_decode($sasKeyValue), TRUE))); $token = "?sv=2017-04-17&ss=b&srt=sco&sp=rwdlac&se=" . $end . "&st=" . $start . "&rscd=" . $rscd . "&spr=" . $spr . "&sig=" . $signature; return $token; 变量无效,as mentioned in the docs.

有什么想法吗?

http://{version}.{unit].{service}.{domain}/

1 个答案:

答案 0 :(得分:0)

正如@Gauralv指出的那样,您应该使用以下格式创建Service SAS:

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

请更改您的代码如下:

$start = (new \DateTime())->setTimezone(new \DateTimeZone('GMT'))->modify('-1 minutes');     
$end = (new \DateTime())->setTimezone(new \DateTimeZone('GMT'))->modify('+10 minutes');
$start = $start->format('Y-m-d\TH:i:s\Z');
$end = $end->format('Y-m-d\TH:i:s\Z');
$rscd = "file; attachment";


$toSign = "rwdl" . "\n";
$toSign .= $start . "\n";
$toSign .= $end . "\n";
$toSign .= "/blob" ."/".$accountName . "/" . $container . "/" . $blob . "\n";
$toSign .= "\n";
$toSign .= "\n"; 
$toSign .= "\n";
$toSign .= "2017-04-17" . "\n";
$toSign .= "\n";
$toSign .= $rscd . "\n";
$toSign .= "\n";
$toSign .= "\n"; 

$signature = rawurlencode(base64_encode(hash_hmac('sha256', $toSign, base64_decode($sasKeyValue), TRUE))); 
$token = "?st=" . $start . "&se=" . $end . "&sr=b&sp=rwdl&sv=2017-04-17&rscd=" . $rscd . "&sig=" . $signature;

return $token;