Retrieve SAS key from a service bus via http request or C# sdk

Ricardo Jacobsen 20 Reputation points
2024-03-07T07:25:35.32+00:00

I need to get some SAS Kay from some service buses and I do it manually very often. I need to implement something that I get the SAS Key by the name. Is it possible to retrieve it via http or with c# sdk or with any other method?

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
632 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,997 questions
{count} votes

Accepted answer
  1. MuthuKumaranMurugaachari-MSFT 22,336 Reputation points
    2024-03-12T15:02:09.2766667+00:00

    Ricardo Jacobsen Thanks for posting your question in Microsoft Q&A. Based on my understanding, you are looking to retrieve primary or secondary keys by key name (not generate SAS token). Correct?

    If so, you can use Namespaces - Authorization Rules - List Keys rest api with key name being authorization rule name in your client app. Make sure that identity you are using has permissions such as Microsoft.ServiceBus/namespaces/authorizationRules/listkeys/action, Microsoft.ServiceBus/namespaces/authorizationRules/read etc. (refer Microsoft.ServiceBus for full set of permissions) or you can assign built-in role such as Azure Service Bus Data Owner to the identity.

    You can also retrieve it using Azure.Messaging.ServiceBus.Administration package for managing entities in an existing namespace and here is the code snippet based on https://stackoverflow.com/a/65404170).

    
    ServiceBusAdministrationClient client = new ServiceBusAdministrationClient(connectionString);
       
                QueueProperties queue =await client.GetQueueAsync("myqueue");
                var sasKey = queue.AuthorizationRules.FirstOrDefault(rule => rule.Name == "sasKeyName")?.PrimaryKey;
    
    
    

    Check ServiceBusAdministrationClient class for other options such as TokenCredential instead of ConnectionString. For other languages, refer https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-management-libraries.

    I hope this helps and let me know if any questions.


    If you found the answer to your question helpful, please take a moment to mark it as Yes for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.