Monday 22 July 2013

Generating Shared Access Signature using Storage API and REST API

When we create a blob container as private, we need to have shared access signature to access a particular blob. The shared access signature can be created either using Storage API or REST API.

When we use Storage client, the storage client assembly needs to be deployed with the application. But when using the RESTful service, it can be used any application support HTTP protocol.

Below is the code for creating SAS token –

First let us create an UI for preparing SAS token
<table width="100%">
    <tr>
        <td>
            <asp:Label ID="lblAccountName" runat="server" Text="Account Name : "></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="txtAccountName" runat="server" Width="400px" Text=""></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="lblContainer" runat="server" Text="Container : "></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="txtContainer" runat="server" Width="400px" Text=""></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="lblSharedKey" runat="server" Text="SharedKey : "></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="txtSharedKey" runat="server" Width="600px" Text=""></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="lblBlob" runat="server" Text="Blob : "></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="txtBlob" runat="server" Width="600px" Text=""></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <asp:TextBox ID="txtSASToken" runat="server" style="width:100%;" TextMode="MultiLine" ReadOnly="true"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <asp:Button ID="btnUsingClient" runat="server" Text="Using Storage Client" Width="200px" OnClick="btnUsingClient_Click" />
            <asp:Button ID="btnUsingREST" runat="server" Text="Using REST" Width="200px" OnClick="btnUsingREST_Click" />
        </td>
    </tr>
</table>

The code for creating SAS token using Storage API
protected void btnUsingClient_Click(object sender, EventArgs e)
{
 var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
 CloudBlobClient client = storageAccount.CreateCloudBlobClient();
 CloudBlobContainer container = client.GetContainerReference(txtContainer.Text);

 string path = container.GetBlobReferenceFromServer(txtBlob.Text).Uri.ToString();

 SharedAccessBlobPermissions permissions = new SharedAccessBlobPermissions();
 permissions = SharedAccessBlobPermissions.Read;

 SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy()
 {
  Permissions = permissions,
  SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5), // To make adjustment on UTC time difference
  SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(60)
 };

 string queryString = container.GetSharedAccessSignature(policy);

 txtSASToken.Text = path + queryString;
}

The code for creating SAS token using Storage API
protected void btnUsingREST_Click(object sender, EventArgs e)
{
 string permissions = "r";
 DateTime startTime = DateTime.UtcNow.AddMinutes(-5); // To make adjustment on UTC time difference
 DateTime expiryTime = startTime.AddMinutes(60);

 string signature = permissions + "\n" +
  startTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ") + "\n" +
  expiryTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ") + "\n" +
  string.Format("/{0}/{1}", txtAccountName.Text, txtContainer.Text) + "\n" +
  String.Empty;

 using (var hmac = new System.Security.Cryptography.HMACSHA256(Convert.FromBase64String(txtSharedKey.Text)))
  signature = Convert.ToBase64String(hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(signature)));

 string strSASToken = string.Format("st={0}&se={1}&sr=c&sp=r&sig={2}",
   Uri.EscapeDataString(startTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")),
   Uri.EscapeDataString(expiryTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")),
   Uri.EscapeDataString(signature));

 txtSASToken.Text = txtBlob.Text + "?" + strSASToken;
}

The output of the code would looks as below


0 Responses to “Generating Shared Access Signature using Storage API and REST API”

Post a Comment