Request authorization in Azure SDK

Introduction

Azure storage requires all client REST requests be “signed”. The storage server validates the signature. The server will reject the request if it is not properly signed.

The goal of this post is to explain how the client-side signing works. That can help when troubleshooting 403 error codes. In addition, you can sign your requests entirely bypassing the client library of the Azure SDK.

Shared key authentication (authorization) in Azure

Client (that is, you) receives a “key”, in a form of Base64 string. The key is sent to you when you sign for Azure services.

The client code takes certain parts of the request in a specific order, and builds (from those parts) a string. I refer to this string as to “signature string”. This string will be the argument to the signing algorithm, the latter computes the “signature”. This computed signature is sent, along with the request, to the server. Needless to say, the shared key itself is not sent.

Thus, the client-side signing procedure is:

-Build a “signature string”, from which the actual signature will be computed,

-Compute signature, using the “signature string” as an argument, and using the shared key

-Send the resulting signature in a form of additional request header.

Building the signature string

By far the most interesting part of the whole signing exercise are rules of building the signature string. I start by listing parts of the request, which go into signature string.

There are two parts.

The first part is the “suffix” of the request URL, which is everything that follows the hostname part of the URL, without the query params starting with “?”.

Part 2 is composed of HTTP request header values, but not all of them. There are two groups of headers that do participate: 1) legacy headers like date, MD5, content type, and 2) Azure-specific headers, which are headers with names starting with “x-ms-”. For example, “x-ms-date”.

For queue and blob storage, the first group can be ignored.

The second group is, in general case, sorted by ascending header name, and concatenated. However, because in all practical cases, we only need one header (which is “x-ms-date”), I ignore the multi-header case here. The gain is extreeme simplicity of the resulting code.

The signature string needs the value of current time, in UTC, and in standard format. The timeis to be retrieved by the code:

The first line converts the signature string to byte array; this is needed because the actual hash-computing code works on byte arrays.

The second line computes the hash.

And the third and the last line just convert the signature to Base64.

Note: the “sharedKey” used in line 2 is computed, once, from the Base64 shared key your received by the code like below, also straightforward:

publicbyte[] Key

{

get { returnConvert.FromBase64String(Base64Key); }

}

How do we actually sign the HTTP request

Azure SDK uses an Azure-specific HTTP header, with the name “Authorization”. Don’t be confused by “authorization” vs. “authentication” – the actual use as per now is for authentication of the client. And authentication/authorization header value is computed as below:

string AuthHeaderValue = string.Format(CultureInfo.InvariantCulture,

"SharedKey {0}:{1}",

accountName,

signature);

Now we all we need to build needed headers for signed HTTP REST request: