我需要使用Microsoft.Azure.ServiceBus 3.X nuget包以编程方式为服务总线创建SaS令牌,以与.NET标准库一起使用。
我可以成功创建并使用令牌来订阅和发布到Service Bus。 我看不到可以限制令牌只能发布的选项。
TokenProvider td = SharedAccessSignatureTokenProvider.CreateSharedAccessSignatureTokenProvider(policyName, policyKey, expireTimeSpan);
var token = await td.GetTokenAsync($"{path}{topic}", expireTimeSpan);
我想限制此令牌的权限,使其只能发布到该主题,而不能订阅。这可能吗,如果可以,我该怎么做?
答案 0 :(得分:1)
这有可能吗?如果可以的话,我该怎么做?
如果我理解正确,则需要创建一个具有[发送]权限的策略。然后使用policyName和生成的密钥创建sas令牌。
策略规则赋予的权利可以是以下各项的组合:
- “发送” -授予向实体发送消息的权利
- “收听” -赋予收听(中继)或接收(队列,订阅)以及所有相关消息处理的权限
- “管理” -授予管理名称空间拓扑的权利,包括创建和删除实体
有关更多信息,请参阅此document。
更新:
我们可以使用Microsoft.Azure.Management.ServiceBus.Fluent来创建策略。
var authorizationRuleName = "xxx"; //policy name
var credentials = SdkContext.AzureCredentialsFactory.FromFile(@"D:\Tom\Documents\azureCred.txt");
var restClient = RestClient.Configure().WithEnvironment(AzureEnvironment.AzureGlobalCloud)
.WithCredentials(credentials)
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Build();
System.Threading.CancellationToken cancellationToken = new System.Threading.CancellationToken();
ServiceBusManagementClient client = new ServiceBusManagementClient(restClient)
{
SubscriptionId = subscriptionId
};
List<AccessRights?> list = new List<AccessRights?> { AccessRights.Send};
//create policy
SharedAccessAuthorizationRuleInner result = client.Namespaces.CreateOrUpdateAuthorizationRuleAsync(resourceGroupName, nameSpace, authorizationRuleName, list, cancellationToken).Result;
//get key
var key = client.Namespaces.ListKeysAsync(resourceGroupName, nameSpace, authorizationRuleName).Result?.PrimaryKey;
如何创建azureCred文件,请参阅此document。