【发布时间】:2015-09-03 20:27:12
【问题描述】:
我正在创建能够将一些 blob 上传到 Azure 存储的 Windows Phone 8.1 Store 应用程序。我不能使用 WindowsAzure.Storage lib(很奇怪),所以我正在尝试使用 REST。我想不通,怎么了。
try
{
string time = DateTime.Now.ToString("R", System.Globalization.CultureInfo.InvariantCulture);
string tosign = "GET\n" +
"\n" + //Content-Encoding
"\n" + //Content-Language
"0\n" + //Content-Length
"\n" + //Content-MD5
"\n" + //Content-Type
"\n" + //Date
"\n" + //If-modified-since
"\n" + //If-match
"\n" + //If-none-match
"\n" + //If-unmodified-since
"\n" + //Range
"x-ms-date:" + time + "\nx-ms-version:2015-02-21\n" + //CanonicalizedHeaders
"/storage_name/\ncomp:list"; //CanonicalizedResource
string hashKey = "DHpNuYG5MXhamfbKmFPClUlNi38QiM2uqIqz07pgvpv2gmXJRwxaMlcV05pFCYsrelGYKPed9QphyJ/YnUrh5w=="; //Primary access key
MacAlgorithmProvider macAlgorithmProvider = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256);
var messageBuffer = CryptographicBuffer.ConvertStringToBinary(tosign, BinaryStringEncoding.Utf8);
IBuffer keyBuffer = CryptographicBuffer.ConvertStringToBinary(hashKey, BinaryStringEncoding.Utf8);
CryptographicKey hmacKey = macAlgorithmProvider.CreateKey(keyBuffer);
IBuffer signedMessage = CryptographicEngine.Sign(hmacKey, messageBuffer);
string hashedString = CryptographicBuffer.EncodeToBase64String(signedMessage);
var client = new HttpClient();
Uri uri = new Uri("https://storage_name.blob.core.windows.net/?comp=list");
client.DefaultRequestHeaders.Add("x-ms-date", time);
client.DefaultRequestHeaders.Add("x-ms-version", "2015-02-21");
client.DefaultRequestHeaders.Add("Authorization", "SharedKey storage_name:" + hashedString);
var response = await client.GetAsync(uri);
}
catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
}
错误:服务器未能验证请求。确保 Authorization 标头的值格式正确,包括签名。
我怎样才能让它发挥作用?
【问题讨论】:
-
我猜,我的加密方法有错误。
标签: c# azure windows-runtime windows-phone-8.1 sha256