简体   繁体   中英

Using Azure.Messaging.ServiceBus with SAS token in netstandard2 project

The Microsoft.Azure.ServiceBus package allows to connect to Azure via SAS tokens. Microsoft recommends to use the newer package Azure.Messaging.ServiceBus instead:

在此处输入图像描述

But I couldn't find a way to create a ServiceBusClient with the help of a SAS token.

Is there a way to create a ServiceBusClient with a SAS token and if yes: how. If not: What is the recommended way to connect to Azure via SAS token? Do I have to use the outdated Microsoft.Azure.ServiceBus package? There is a third package named WindowsAzure.ServiceBus but it seems that this one needs a .NET Framework project and does not work with .NET Standard?

Note: We cannot use the authentication via Azure.Identity because the users who should get access to the service bus are not part of our Azure AD and never will.

Thank you in advance.

Using a SAS token with the ServiceBusClient can be done by adding your SAS token to the connection string, using the pattern SharedAccessSignature=<<SAS>>; and removing the SharedAccessKey and SharedAccessKeyName tokens that the portal populates.

In the 7.2.x-beta line, a SAS token can also be specified by using the constructor that accepts an AzureSasCredential :

var credential = new AzureSasCredential("<< YOUR SAS TOKEN >>");

var fullyQualifiedNamespace = 
    "<< NAMESPACE (likely similar to {your-namespace}.servicebus.windows.net) >>";

await using var client = new ServiceBusClient(fullyQualifiedNamespace, credential);

If you're able to make use of the beta, I'd recommend using the credential, as it allows you to update the SAS token without creating a new client, which the connection string approach does not support.

With respect to libraries, Azure.Messaging.ServiceBus is the current generation and we'd recommend using it over the two legacy libraries that you've mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM