1
votes

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?

enter image description here

1
Can you please share how are you calculating the authorization header value? My guess is you're missing out on a simple thing there.Gaurav Mantri
@GauravMantri Indeed the issue was with the calculations of the authorization header value.tRuEsatm

1 Answers

2
votes

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