1
votes

I am using new Azure ServiceBus SDK which is .NET Standard 2.0 and I am finding it difficult to use SAS Token. Previously, it used to be MessagingFactory but in new SDK it's not there. ServiceBusConnectionStringBuilder has SAS Token but it also expects a connectionString.

Basically, I want to Send and Receive using SAS Policy (Send rule for Sending and Receive rule for Receiving) and SAS token created from connection string of these policies.

I am able to generate SAS Token but cannot find a way to create QueueClient using this token.

1
If I am not mistaken, you would still need to create a connection string from the SAS policy.Gaurav Mantri
@GauravMantri Yes, connection strings can be used but then I won't be able to use SAS Token. I have created SAS Token using connection string.Amit
That's the whole idea. You create a SAS Policy and the use either its primary or secondary key to create a connection string in the following format: Endpoint=sb://<namespacename>.servicebus.windows.net/;SharedAccessKeyName=<sas-policy-name>;SharedAccessKey=<sas-key>.Gaurav Mantri
It still uses SAS Key associated with policy which do not have an expiry. Where is the use of SAS token ? I want to use an expiring SAS token generated from .NET Program. Format of which is like this SharedAccessSignature sig=<signature-string>&se=<expiry>&skn=<keyName>&sr=<URL-encoded-resourceURI> Amit
Sorry, my bad! I misunderstood.Gaurav Mantri

1 Answers

2
votes

I ended up using following override of ServiceBusConnectionStringBuilder that uses a SharedAccess Signature:

public ServiceBusConnectionStringBuilder (string endpoint, string entityPath, string sharedAccessSignature);

Based on this, here's the code I wrote. This first generates a SAS token using RootManagedAccessKey that is valid for an hour and then uses that token to send a message to a queue.

using System;
using System.Text;
using Microsoft.Azure.ServiceBus;
using Microsoft.Azure.ServiceBus.Primitives;

namespace SO60273377
{
    class Program
    {
        static void Main(string[] args)
        {
            var endpoint = "sb://<namespace>.servicebus.windows.net/";
            var queueName = "test";
            var keyName = "RootManageSharedAccessKey";
            var keyValue = "<key>";
            var validityDuration = TimeSpan.FromHours(1);

            TokenScope tokenScope = TokenScope.Entity;

            var provider = (SharedAccessSignatureTokenProvider) TokenProvider.CreateSharedAccessSignatureTokenProvider(keyName, keyValue, validityDuration, tokenScope);

            var token = provider.GetTokenAsync(endpoint+queueName, validityDuration).GetAwaiter().GetResult();
            var sasToken = token.TokenValue;
            Console.WriteLine("SAS Token: " + sasToken);
            var serviceBusConnectionStringBuilder = new ServiceBusConnectionStringBuilder(endpoint, queueName, sasToken);
            QueueClient client = new QueueClient(serviceBusConnectionStringBuilder, ReceiveMode.PeekLock);
            client.SendAsync(new Message(Encoding.UTF8.GetBytes("This is a test"))).GetAwaiter().GetResult();


            Console.WriteLine("Press any key to continue");
            Console.ReadLine();
        }
    }
}