Search code examples
c#azureazureservicebus.net-standardazure-management-api

Create Azure Service Bus policy with multiple claims in Azure Management libraries for .NET


I'm using the Azure Management libraries for .NET with Azure Service Bus. My objective is to create an Shared access policy with Send and Listen claims. I can do this in the Azure Portal.

Policy created in the azure portal

IServiceBusNamespace serviceBus = ...
IBlank policyDefinition = serviceBus.AuthorizationRules.Define("MySendAndListenPolicy2");

From IBlank I can call WithListeningEnabled() or WithSendingEnabled(). Both return an IWithCreate, which I can only create the rule from (i.e. Create() and CreateAsync(). There doesn't seem to be an option to configure both Send and Listen.

How can I create a policy with Send and Listen using the Azure Management .NET SDK?


Solution

  • I've achieved this by using the Inner objects of the fluent API.

    // using Microsoft.Azure.Management.Fluent;
    // using Microsoft.Azure.Management.ServiceBus.Fluent;
    // using Microsoft.Azure.Management.ServiceBus.Fluent.Models;
    
    string policyName = "ListenAndSendPolicy";
    
    // Define the claims
    IList<AccessRights?> rights = new List<AccessRights?>();
    rights.Add(AccessRights.Send);
    rights.Add(AccessRights.Listen);
    
    // Create the rule
    SharedAccessAuthorizationRuleInner rule = await serviceBus.AuthorizationRules.Inner.CreateOrUpdateAuthorizationRuleAsync(
        serviceBus.ResourceGroupName,
        serviceBus.Name,
        policyName,
        rights);
    
    // Get the policy
    policy = await serviceBus.AuthorizationRules.GetByNameAsync(policyName);
    

    This successfully created the policy

    enter image description here