As most people probably reading this blog post have already discovered, the Azure Service Bus Queue is a very powerful tool that can be used for a variety of scenarios. If you have a .NET application that is communicating with the Service Bus Queue, then chances are you are using this NuGet package to do so. But then this begs the question: “What if I am not using .NET but I still need to communicate with the Service Bus Queue”?
The good news is that in addition to a .NET client, there also exists a REST client for communicating with the queue. The bad news is that in the request header we need to pass through a Shared Access Signature (SAS) authorization token, and Azure does not provide any “out of box” way of retrieving this token. If you attempt to communicate with the queue without this token, you will get a 401 unauthorized response. This blog post will outline how you can create a simple C# console application to generate this token for you.
As stated earlier there is nothing on any of the Azure portals that will get you this SAS token directly which means that we will have to generate it manually given the information that Azure does supply. We will do so using .NET and more specifically we will use the Azure Service Bus NuGet package shown below:
This package contains useful methods that can help us easily generate a SAS token for our other applications. While we may not necessarily need this package not doing so would require far more effort and would require us to use an HMAC-SHA256 hash to do so. For this blog post we will keep it simple and use the built in methods in the Azure Service Bus NuGet package.
So in a newly create console app add the following code to the project:
In this code replace all the variables with the following:
- sbNamespace: The namespace of the Azure Service Bus that you created
- sbPath: The name of the queue that you created
- sbPolicy: The Shared Access Key Name
- sbKey: The Shared Access Key
Once you run this console app, navigate to the location specified on the last line and you should now see the text file which when opened should contain a SAS token similar to the one shown below:
And there you have it! An easily configurable console app that generates a SAS token for your HTTP requests to the Azure Service Bus Queue.