エンティティを Azure テーブルに挿入するためにすべてを試してみましたが、これまでのところ、同じエラー「StatusCode: 403, ReasonPhrase: 'Server failed to authenticate the request. Make sure the value of Authorization header.署名を含めて正しく形成されています。」
今、私はShareKeyとSharedKeyLiteを使用しようとしました(Azure Storage ExplorerはSharedKeyLiteを使用しています)
public static async Task<string> InsertEntityAsync(string tableName, Position position)
{
string uri = @"https://" + Utilities.Account + ".table.core.windows.net/" + tableName;
return await Utilities.UploadEntityAsync(tableName,uri,position);
}
public static async Task<string> UploadEntityAsync(string urlPath, string uri, Position position)
{
string body = buildBodyForInsertOperation(position);
HttpClient request = new HttpClient();
string formatedTime = Authentication.FormatedTime();
request.DefaultRequestHeaders.Add("x-ms-date", formatedTime);
//Adding the Authorization header to the request
string authorization = Authentication.GetSignedString("POST",formatedTime, urlPath, Utilities.Account, Utilities.Key);
request.DefaultRequestHeaders.Add("Authorization", authorization);
request.DefaultRequestHeaders.TryAddWithoutValidation("Content-Length", body.Length.ToString());
HttpResponseMessage messageResult = await request.PostAsync(uri, new StringContent(body, UTF8Encoding.UTF8, "application/atom+xml"));
return messageResult.ToString();
}
public static string GetSignedString(string httpMethod, string time, string urlPath, string account, string key)
{
String contentMD5 = String.Empty;
String contentType = "application/atom+xml";
String canonicalizedResource = String.Format("/{0}/{1}", account, urlPath);
String stringToSign = String.Format(
"{0}\n{1}\n{2}\n{3}\n{4}",
httpMethod,
contentMD5,
contentType,
time,
canonicalizedResource);
string signedKey = SignThis(stringToSign, key, account);
return signedKey;
}
private static String SignThis(String canonicalizedString,string Key, string Account)
{
String signature = string.Empty;
byte[] unicodeKey = Convert.FromBase64String(Key);
using (HMACSHA256 hmacSha256 = new HMACSHA256(unicodeKey))
{
Byte[] dataToHmac = System.Text.Encoding.UTF8.GetBytes(canonicalizedString);
signature = Convert.ToBase64String(hmacSha256.ComputeHash(dataToHmac));
}
String authorizationHeader = String.Format(
CultureInfo.InvariantCulture,
"{0} {1}:{2}",
"SharedKeyLite",
Account,
signature);
return authorizationHeader;
}
時間パラメーターは、Azure が必要とするものに従ってフォーマットされますが、他に何をすべきか、何を試してよいかわかりません。httpMethod、contentMD5、content-type、およびあらゆる種類の組み合わせなしでリクエストを作成しようとしましたが、それでもまだです。
私は、SignThis(...) メソッドが機能することを確信しています。これを使用して、エンティティをクエリするための GET 要求にも署名しているため、ヘルプや言葉があれば大いに役立ちます。ありがとう
/編集済み / UploadEntityAsync メソッドをアタッチしています。私の場合、Azure に Position というテーブルがあるため、XML を構築しています。とにかく、それは間違っているわけではありません。構築した XML を Azure の XML と比較したからです。 Fiddler を使用するストレージ エクスプローラーは問題ありません。唯一の問題は署名です