Important: In this sample the secret key is present on the client (Phone) itself. SInce a secret key can never be stored safely on phone, on a production system, the users should be authenticated using a identity provider to get a user token that can be used to get an authorization token from ACS.
Many a time you might want to interact with Azure servicebus, but may not have the privilage to use the service bus dll. In such a scenario you would need to make use of Azure Servicebus REST API. A best example for this scenario would be connecting to Azure service bus from Windows Phone 7. As you may already know, you cannot make use of the service bus dll in a phone. In this post, I will walk you though every step required to complete the integration between Azure Service Bus Queue and your client REST API.
Step 1 : Set up your Azure Service Bus and Access Control Service (ACS)
Create a service bus namespace and an associate ACS namespace in your Azure account. Get the secret key from "Default Key" in your Azure service bus account. We will use this later to connect to Azure from your client application.
Step 2 : From client, get autentication token to Azure using REST
In this step we will get an authentication token from Azure and attach it in the header of all subsequent requests to interact with the service bus. To get an authentication token, you will need the issuer password (the secret key you got in last step), the issuer name (almost alway "owner") and your service namespace (the namespace for servicebus and ACS). The code listing for the same is given below. The ACS base address may change in future Azure releases. Rest of the code listing is self explanatory.
private void GetAuthenticationToken()
string acsBaseAddress = "https://" + ServiceNamespace + "-sb.accesscontrol.windows.net/WRAPv0.9/";
// Create the body of the Post Request, formatted as a HTTP Form
string postData = "wrap_scope=" + Uri.EscapeDataString("http://"+ ServiceNamespace +".servicebus.windows.net/") + "&wrap_name="
+ Uri.EscapeDataString(IssuerName) + "&wrap_password="
var webClient = new WebClient();
webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_GettingTokenCompleted);
webClient.UploadStringAsync(new Uri(acsBaseAddress), "POST", postData);
Step 3 : Extract the token and append it to the header of every subsequent requests
The result of the token request in step 2 arrives in a particular string format with '&' as the delimiter. To obtain the actual token, refer to the code snippet below.
private void webClient_GettingTokenCompleted(object sender, UploadStringCompletedEventArgs e)
string tokenVariables = e.Result.Split('&');
string tokenVariable = tokenVariables.Split('=');
string authenticationToken = Uri.UnescapeDataString(tokenVariable);
Step 4 : Perform service bus operations using the authentiction token
Once you have the authentication token, append it to the header called "Authentication" to interact with the service bus. In the code snippet below, I will post a message to the service bus queue.
var webClient = new WebClient();
webClient.Headers["Authorization"] = "WRAP access_token=\"" + authenticationToken + "\"";
webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_SendMessageCompleted);
new Uri(FullAddress), "POST", Body);
Similary you can perform any interacation with the service bus. You may also refer to MSDN REST API reference. Also do leave a comment with your questions/feedback and I would be happy to respond. Happy coding!