Create a Windows Forms Application to Display the Shared
Access Permissions Signature
-
Open Microsoft Visual Studio 2010 and create a blank
solution.
-
Right-click the solution and select Add | New
Project.
-
Select Windows Forms Application, provide a name for the
application (such as BLOBUriGenerator), and click OK.
-
Right-click the project and select Add References. Add
Microsoft.WindowsAzure.StorageClient.dll
(from C:\Program Files\Windows Azure SDK\v1.3\bin).
-
Click View | Toolbox, drag two buttons onto the Windows
Forms designer surface, and then drag a textbox control onto the
designer surface. Your Windows Forms UI should look similar to
the graphic shown here. Name the buttons btnGetData and btnExit. Name the textbox control txtbxURI.
-
Right-click the Form1.cs in the Solution Explorer and
select View Code.
-
In the code-behind, amend the code by using the bolded
code shown here:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure;
namespace BLOBUrlGenerator
{
public partial class Form1 : Form
{
CloudBlobContainer azureBlobContainer = null;
CloudStorageAccount azureStorageAcct = null;
CloudBlobClient azureBlobClient = null;
BlobContainerPermissions azureBlobPermissions = null;
CloudBlob azureBlob = null;
string submissionDateTime = "";
public Form1()
{
InitializeComponent();
InitializeStorage();
}
private void InitializeStorage()
{
var accountInfo = "DefaultEndpointsProtocol=http;Accountname=fabrikaminc;
AccountKey=<your account key here>
azureStorageAcct = CloudStorageAccount.Parse(accountInfo);
azureBlobClient = azureStorageAcct.CreateCloudBlobClient();
azureBlobContainer = azureBlobClient.GetContainerReference(
"c10testcontainer");
azureBlobContainer.CreateIfNotExist();
azureBlob = azureBlobContainer.GetBlobReference("c10testblob");
azureBlob.UploadText("Test Text");
}
private void btnGetURI_Click(object sender, EventArgs e)
{
SharedAccessPolicy tempAccess = new SharedAccessPolicy();
tempAccess.SharedAccessStartTime = DateTime.Now;
tempAccess.SharedAccessExpiryTime = DateTime.Now.AddHours(48);
tempAccess.Permissions = SharedAccessPermissions.Read;
BlobContainerPermissions tempPermissions = new BlobContainerPermissions();
tempPermissions.SharedAccessPolicies.Clear();
tempPermissions.SharedAccessPolicies.Add("John Doe", tempAccess);
azureBlobContainer.SetPermissions(tempPermissions);
string sharedAccessSig = azureBlobContainer.
GetSharedAccessSignature(tempAccess);
string sharedAccessURI = azureBlobContainer.Uri.AbsoluteUri +
sharedAccessSig;
txtbxURI.Text = sharedAccessURI;
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
The preceding code uses the
Microsoft.WindowsAzure.StorageClient
library to programmatically interact with a Windows Azure BLOB
storage account. You’ll note that there are several class-level
declarations in the snippet, which represent the different
storage objects that you need when interacting with Windows
Azure BLOB storage. Then, as was discussed earlier in this
section, you use the SharedAccessPolicy
class to create a temporary shared access permissions signature, which can be
used to download data and information from the BLOB. The
shared access permissions URI that is created
(sharedAccessURI) is then displayed in the
textbox property.
-
After you finish adding the code, press F5 to debug the
application. You should see something similar to the
following—which exposes the shared access permissions signature in the form of
a URI.
Though this application is fairly simple, it illustrates how
you can create controlled access to BLOB storage resources. This code
within the simple Windows Forms code can equally apply to SharePoint
Web Parts (or other SharePoint artifacts) and Silverlight
applications that can be hosted in SharePoint.
Note
Although Windows Azure storage provides shared access permissions, the use of shared access
permissions is less apparent with the other parts of the platform.
The Windows Azure AppFabric platform, for example, employs a
different type of security called the Access Control Service, which uses
claims to authenticate users and applications.