Can't delete Azure Storage Table using Azure REST API

梦想与她 提交于 2019-12-11 06:25:46

问题


I am getting an error "Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature."

I followed the authorization tutorial provided by Microsoft, Delete Table, Authentication for the Azure Storage Services.

Am I missing anything?


回答1:


It seems that you’d like to delete table via rest api.

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

the following sample works fine on my side, please refer to the code to generate the signature.

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;
}


来源:https://stackoverflow.com/questions/41558785/cant-delete-azure-storage-table-using-azure-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!