无法使用Azure REST API删除Azure存储表

时间:2017-01-09 23:34:46

标签: azure azure-storage azure-table-storage

我收到错误“服务器无法验证请求。请确保正确形成授权标头的值,包括签名。

我按照Microsoft提供的授权教程Delete TableAuthentication for the Azure Storage Services

我错过了什么吗?

enter image description here

1 个答案:

答案 0 :(得分:2)

您似乎想要delete table via rest api

  

删除https://myaccount.table.core.windows.net/Tables('mytable')

以下示例在我这方面工作正常,请参阅代码以生成签名。

string StorageAccount = "account name here";
string StorageKey = "account key here";
string tablename = "table name";

string requestMethod = "DELETE";
string mxdate = "";
string storageServiceVersion = "2015-12-11";

protected void Button1_Click(object sender, EventArgs e)
{
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(string.Format(CultureInfo.InvariantCulture,
    "https://{0}.table.core.windows.net/Tables('{1}')",
    StorageAccount, tablename));

    req.Method = requestMethod;

    //specify request header
    string AuthorizationHeader = generateAuthorizationHeader();
    req.Headers.Add("Authorization", AuthorizationHeader);
    req.Headers.Add("x-ms-date", mxdate);
    req.Headers.Add("x-ms-version", storageServiceVersion);
    req.ContentType = "application/json";

    req.Accept = "application/json;odata=minimalmetadata";

    using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
    {

    }
}

public string generateAuthorizationHeader()
{
    mxdate = DateTime.UtcNow.ToString("R");

    string canonicalizedResource = $"/{StorageAccount}/Tables('{tablename}')"; 

    string contentType = "application/json";

    string stringToSign = $"{requestMethod}\n\n{contentType}\n{mxdate}\n{canonicalizedResource}";  

    HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(StorageKey));

    string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));

    String authorization = String.Format("{0} {1}:{2}",
        "SharedKey",
        StorageAccount,
        signature
        );

    return authorization;
}