Freigeben über


Windows Phone 7 & Azure Service Bus REST Authentication and Messaging

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("https://"+ ServiceNamespace +".servicebus.windows.net/") + "&wrap_name="
 + Uri.EscapeDataString(IssuerName) + "&wrap_password="
 + Uri.EscapeDataString(IssuerPassword);
 
 string authorizationToken;
 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[0].Split('=');
 string authenticationToken = Uri.UnescapeDataString(tokenVariable[1]);
 }

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);
 webClient.UploadStringAsync(
 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!

Comments

  • Anonymous
    March 19, 2013
    How can i find my Full Address?

  • Anonymous
    March 19, 2013
    The 'Full Address' is your service bus uri. You will get it from your Azure service bus settings. Typically it would look something like this "https://<namespace>.servicebus.windows.net/Incoming/messages";