%3CLINGO-SUB%20id%3D%22lingo-sub-1171377%22%20slang%3D%22en-US%22%3EHow%20to%20send%20messages%20to%20azure%20service%20bus%20queue%20using%20rest%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1171377%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHello%20there%2C%20folks!%20I%E2%80%99d%20like%20to%20share%20a%20C%23%20code%20snippet%20and%20PowerShell%20script%20which%20can%20be%20helpful%20in%20getting%20SAS%20token%20and%20use%20it%20in%20scenario%20sending%20message%20to%20service%20bus%20queue%20but%20with%20rest%20calls.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBelow%20is%20the%20method%20by%20which%20we%20can%20generate%20SAS%20token%3A-%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CU%3EC%23%20Sample%20for%20SAS%20token%20generate%3A%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20static%20void%20Main(string%5B%5D%20args)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%7B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20string%20queueUrl%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2F%253cyournamespace%253e.servicebus.windows.net%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2F%3CYOURNAMESPACE%3E.servicebus.windows.net%2F%3C%2FYOURNAMESPACE%3E%3C%2FA%3E%22%20%2B%20%22%3CYOURQUEUENAME%3E%22%3B%3C%2FYOURQUEUENAME%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20string%20token%20%3D%20GetSasToken(queueUrl%2C%20%22%3CYOURACCESSKEYNAME%3E%22%2C%20%22%3CYOURACCESSKEYVALUE%3E%22%2C%20TimeSpan.FromDays(1))%3B%3C%2FYOURACCESSKEYVALUE%3E%3C%2FYOURACCESSKEYNAME%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%7D%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20public%20static%20string%20GetSasToken(string%20resourceUri%2C%20string%20keyName%2C%20string%20key%2C%20TimeSpan%20ttl)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%7B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20var%20expiry%20%3D%20GetExpiry(ttl)%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20string%20stringToSign%20%3D%20HttpUtility.UrlEncode(resourceUri)%20%2B%20%22%5Cn%22%20%2B%20expiry%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20HMACSHA256%20hmac%20%3D%20new%20HMACSHA256(Encoding.UTF8.GetBytes(key))%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20var%20signature%20%3D%20Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)))%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20var%20sasToken%20%3D%20String.Format(CultureInfo.InvariantCulture%2C%20%22SharedAccessSignature%20sr%3D%7B0%7D%26amp%3Bsig%3D%7B1%7D%26amp%3Bse%3D%7B2%7D%26amp%3Bskn%3D%7B3%7D%22%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20HttpUtility.UrlEncode(resourceUri)%2C%20HttpUtility.UrlEncode(signature)%2C%20expiry%2C%20keyName)%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20return%20sasToken%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%7D%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20private%20static%20string%20GetExpiry(TimeSpan%20ttl)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%7B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20TimeSpan%20expirySinceEpoch%20%3D%20DateTime.UtcNow%20-%20new%20DateTime(1970%2C%201%2C%201)%20%2B%20ttl%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20return%20Convert.ToString((int)expirySinceEpoch.TotalSeconds)%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%7D%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CU%3EPowerShell%20Script%20for%20SAS%20token%20generate%3A%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%5BReflection.Assembly%5D%3A%3ALoadWithPartialName(%22System.Web%22)%7C%20out-null%3C%2FP%3E%0A%3CP%3E%23Parameter%3C%2FP%3E%0A%3CP%3E%24uri%20%3D%20'https%3A%2F%2F%3CYOURNAMESPACE%3E.servicebus.windows.net%2F%3CYOURQUEUENAME%3E'%3C%2FYOURQUEUENAME%3E%3C%2FYOURNAMESPACE%3E%3C%2FP%3E%0A%3CP%3E%24Access_Policy_Name%3D%22%3CYOURACCESSPOLICYNAME%3E%22%3C%2FYOURACCESSPOLICYNAME%3E%3C%2FP%3E%0A%3CP%3E%24Access_Policy_Key%3D%22youraccesskeyvalue%22%3C%2FP%3E%0A%3CP%3E%23Token%20expires%20now%2B300%3C%2FP%3E%0A%3CP%3E%24Expires%3D(%5BDateTimeOffset%5D%3A%3ANow.ToUnixTimeSeconds())%2B300%3C%2FP%3E%0A%3CP%3E%23Building%20Token%3C%2FP%3E%0A%3CP%3E%24SignatureString%3D%5BSystem.Web.HttpUtility%5D%3A%3AUrlEncode(%24URI)%2B%20%22%60n%22%20%2B%20%5Bstring%5D%24Expires%3C%2FP%3E%0A%3CP%3E%24HMAC%20%3D%20New-Object%20System.Security.Cryptography.HMACSHA256%3C%2FP%3E%0A%3CP%3E%24HMAC.key%20%3D%20%5BText.Encoding%5D%3A%3AASCII.GetBytes(%24Access_Policy_Key)%3C%2FP%3E%0A%3CP%3E%24Signature%20%3D%20%24HMAC.ComputeHash(%5BText.Encoding%5D%3A%3AASCII.GetBytes(%24SignatureString))%3C%2FP%3E%0A%3CP%3E%24Signature%20%3D%20%5BConvert%5D%3A%3AToBase64String(%24Signature)%3C%2FP%3E%0A%3CP%3E%24SASToken%20%3D%20%22SharedAccessSignature%20sr%3D%22%20%2B%20%5BSystem.Web.HttpUtility%5D%3A%3AUrlEncode(%24URI)%20%2B%20%22%26amp%3Bsig%3D%22%20%2B%20%5BSystem.Web.HttpUtility%5D%3A%3AUrlEncode(%24Signature)%20%2B%20%22%26amp%3Bse%3D%22%20%2B%20%24Expires%20%2B%20%22%26amp%3Bskn%3D%22%20%2B%20%24Access_Policy_Name%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%24headerParams%20%3D%20%40%7B'Authorization'%3D%22%24(%24SASToken)%22%7D%3C%2FP%3E%0A%3CP%3E%24headerParams%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditormanishshar_0%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSTRONG%3EPS%20Output%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Sas.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F171044i27779820F25B6412%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Sas.png%22%20alt%3D%22SAS%20Token%20output%20from%20PSScript%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3ESAS%20Token%20output%20from%20PSScript%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFinally%2C%20you%20can%20make%20a%20call%20to%20service%20bus%20queue%20to%20create%20message%20using%20any%20Http%20client.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22postcall.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F171045i23E496BE32686930%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22postcall.png%22%20alt%3D%22POST%20call%20to%20create%20message%20in%20service%20bus%20queue%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EPOST%20call%20to%20create%20message%20in%20service%20bus%20queue%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EReference%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Frest%2Fapi%2Fservicebus%2Fsend-message-to-queue%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Frest%2Fapi%2Fservicebus%2Fsend-message-to-queue%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fservice-bus-messaging%2Fservice-bus-sas%23use-the-shared-access-signature-at-http-level%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fservice-bus-messaging%2Fservice-bus-sas%23use-the-shared-access-signature-at-http-level%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%3C%2FLINGO-BODY%3E
Microsoft

 

Hello there, folks! I’d like to share a C# code snippet and PowerShell script which can be helpful in getting SAS token and use it in scenario sending message to service bus queue but with rest calls.

 

Below is the method by which we can generate SAS token:-

C# Sample for SAS token generate:

 

        static void Main(string[] args)

        {

            string queueUrl = "https://<yournamespace>.servicebus.windows.net/" + "<yourqueuename>";

            string token = GetSasToken(queueUrl, "<youraccesskeyname>", "<youraccesskeyvalue>", TimeSpan.FromDays(1));

        }

        public static string GetSasToken(string resourceUri, string keyName, string key, TimeSpan ttl)

        {

            var expiry = GetExpiry(ttl);

            string stringToSign = HttpUtility.UrlEncode(resourceUri) + "\n" + expiry;

            HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));

            var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));

            var sasToken = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}",

            HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), expiry, keyName);

            return sasToken;

        }

 

        private static string GetExpiry(TimeSpan ttl)

        {

            TimeSpan expirySinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1) + ttl;

            return Convert.ToString((int)expirySinceEpoch.TotalSeconds);

        }

       

PowerShell Script for SAS token generate:

[Reflection.Assembly]::LoadWithPartialName("System.Web")| out-null

#Parameter

$uri = 'https://<yournamespace>.servicebus.windows.net/<yourqueuename>'

$Access_Policy_Name="<youraccesspolicyname>"

$Access_Policy_Key="youraccesskeyvalue"

#Token expires now+300

$Expires=([DateTimeOffset]::Now.ToUnixTimeSeconds())+300

#Building Token

$SignatureString=[System.Web.HttpUtility]::UrlEncode($URI)+ "`n" + [string]$Expires

$HMAC = New-Object System.Security.Cryptography.HMACSHA256

$HMAC.key = [Text.Encoding]::ASCII.GetBytes($Access_Policy_Key)

$Signature = $HMAC.ComputeHash([Text.Encoding]::ASCII.GetBytes($SignatureString))

$Signature = [Convert]::ToBase64String($Signature)

$SASToken = "SharedAccessSignature sr=" + [System.Web.HttpUtility]::UrlEncode($URI) + "&sig=" + [System.Web.HttpUtility]::UrlEncode($Signature) + "&se=" + $Expires + "&skn=" + $Access_Policy_Name

 

$headerParams = @{'Authorization'="$($SASToken)"}

$headerParams

 

PS Output:

SAS Token output from PSScriptSAS Token output from PSScript

 

Finally, you can make a call to service bus queue to create message using any Http client.

 

POST call to create message in service bus queuePOST call to create message in service bus queue

 

Reference: