为了将实体插入 Azure 表,我已经完成并尝试了所有操作,但到目前为止,我仍然收到相同的错误“StatusCode:403,ReasonPhrase:'服务器无法对请求进行身份验证。确保授权标头的值正确形成,包括签名。'"
现在,我尝试使用 ShareKey 和 SharedKeyLite(Azure 存储资源管理器使用 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 的一个进行了比较使用 Fiddler 的存储资源管理器,一切正常。唯一的问题是签名