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.