在.NET的Azure管理库中创建具有多个声明的Azure服务总线策略

时间:2018-11-26 10:35:36

标签: c# azure azureservicebus .net-standard azure-management-api

我正在将Azure Management libraries for .NET与Azure Service Bus一起使用。我的目标是使用 Send Listen 声明创建共享访问策略。我可以在Azure门户中做到这一点。

Policy created in the azure portal

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

IBlank中,我可以呼叫WithListeningEnabled()WithSendingEnabled()。两者都返回一个IWithCreate,我只能从中创建规则(即Create()CreateAsync()。似乎没有一个选项可以同时配置 Send Listen

如何使用Azure Management .NET SDK通过发送收听创建策略?

1 个答案:

答案 0 :(得分:1)

我已经通过使用流畅的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);

这成功创建了政策

enter image description here