Create Azure Service Bus queue Shared Access Policy programmatically

1.4k views Asked by At

I have implemented an Azure Service Bus REST API client. At the moment I am building xUnit tests for my REST project and need to create a Queue with a name provided by the test, send messages with the REST client and then delete the Queue with that specific name.

One of my requirements are to specify a Shared Access Policy for the newly created Queue with only Send permissions programmatically but I can't find anything online that suggests that this is possible.

So far I have this

TokenProvider credentials = TokenProvider.CreateSharedAccessSignatureTokenProvider("MyBusAccessPolicy", "XXXXXXXXXXXXXXXX");
NamespaceManager namespaceManager = new NamespaceManager(ServiceBusEnvironment.CreateServiceUri("sb", _serviceNamespace, string.Empty), credentials);
QueueDescription queueDescription =  await namespaceManager.CreateQueueAsync(queueName);

How would I proceed to create the Shared Access Policy specifically for that queue if even possible?

1

There are 1 answers

1
Seth Manheim - MSFT On BEST ANSWER

Neil,

Something like this should work:

string queuePolicyName = "SendPolicy";
string queuePrimaryKey = SharedAccessAuthorizationRule.GenerateRandomKey();

QueueDescription queueDescription = new QueueDescription(queueName);
SharedAccessAuthorizationRule queueSharedAccessPolicy = new SharedAccessAuthorizationRule(queuePolicyName, queuePrimaryKey, new[] { AccessRights.Send });
queueDescription.Authorization.Add(queueSharedAccessPolicy);

await _namespaceManager.CreateQueueAsync(queueDescription);