Generate Sas Key Service Bus

// In a typical scenario, you would create a new Azure key (for the service bus) // in the Azure portal, such that the key has limited permissions. This would // allow you to give the SAS token to others for specific access for some period of time. You use the rule's name and key via the Service Bus SDK or in your own code to generate a SAS token. A client can then pass the token to Service Bus to prove authorization for the requested operation. !NOTE Azure Service Bus supports authorizing access to a Service Bus namespace and its entities using Azure Active Directory (Azure AD).

Shared Access Signature (SAS) authentication enables applications to authenticate to Service Bus using an access key configured on the namespace, or on the messaging entity (queue or topic) with which specific rights are associated. You can then use this key to generate a SAS token that clients can in turn use to authenticate to Service Bus.

SAS authentication support is included in the Azure SDK version 2.0 and later. For more information about Service Bus authentication, see Service Bus Authentication and Authorization.

Concepts

SAS authentication in Service Bus involves the configuration of a cryptographic key with associated rights on a Service Bus resource. Clients claim access to Service Bus resources by presenting a SAS token. This token consists of the resource URI being accessed, and an expiry signed with the configured key.

You can configure Shared Access Signature authorization rules on Service Bus relays, queues, topics, and Event Hubs.

SAS authentication uses the following elements:

  • Shared Access authorization rule: A 256-bit primary cryptographic key in Base64 representation, an optional secondary key, and a key name and associated rights (a collection of Listen, Send, or Manage rights).

  • Shared Access Signature token: Generated using the HMAC-SHA256 of a resource string, consisting of the URI of the resource that is accessed and an expiry, with the cryptographic key. The signature and other elements described in the following sections are formatted into a string to form the SharedAccessSignature token.

Configuration for Shared Access Signature authentication

You can configure the SharedAccessAuthorizationRule rule on Service Bus namespaces, queues, or topics. Configuring a SharedAccessAuthorizationRule on a Service Bus subscription is currently not supported, but you can use rules configured on a namespace or topic to secure access to subscriptions. For a working sample that illustrates this procedure, see the Using Shared Access Signature (SAS) authentication with Service Bus Subscriptions sample.

A maximum of 12 such rules can be configured on a Service Bus namespace, queue, or topic. Rules that are configured on a Service Bus namespace apply to all entities in that namespace.

In this figure, the manageRuleNS, sendRuleNS, and listenRuleNS authorization rules apply to both queue Q1 and topic T1, while listenRuleQ and sendRuleQ apply only to queue Q1 and sendRuleT applies only to topic T1.

The key parameters of a SharedAccessAuthorizationRule are as follows:

ParameterDescription
KeyNameA string that describes the authorization rule.
PrimaryKeyA base64-encoded 256-bit primary key for signing and validating the SAS token.
SecondaryKeyA base64-encoded 256-bit secondary key for signing and validating the SAS token.
AccessRightsA list of access rights granted by the authorization rule. These rights can be any collection of Listen, Send, and Manage rights.

When a Service Bus namespace is provisioned, a SharedAccessAuthorizationRule, with KeyName set to RootManageSharedAccessKey, is created by default. Two default SharedAccessAuthorizationRule objects are also configured for notification hubs: one with Listen, Send, and Manage rights, and another with only Listen rights.

Regenerate and revoke keys for Shared Access Authorization rules

It is recommended that you periodically regenerate the keys used in the SharedAccessAuthorizationRule object. Applications should generally use the PrimaryKey to generate a SAS token. When regenerating the keys, you should replace the SecondaryKey with the old primary key, and generate a new key as the new primary key. This enables you to continue using tokens for authorization that were issued with the old primary key, and that have not yet expired.

If a key is compromised and you have to revoke the keys, you can regenerate both the PrimaryKey and the SecondaryKey of a SharedAccessAuthorizationRule, replacing them with new keys. This procedure invalidates all tokens signed with the old keys.

Generating a Shared Access Signature token

Any client that has access to the signing keys specified in the shared access authorization rule can generate the SAS token. It is formatted as follows:

The signature for the SAS token is computed using the HMAC-SHA256 hash of a string-to-sign with the PrimaryKey property of an authorization rule. The string-to-sign consists of a resource URI and an expiry, formatted as follows:

Note that you should use the encoded resource URI for this operation. The resource URI is the full URI of the Service Bus resource to which access is claimed. For example, http://<namespace>.servicebus.windows.net/<entityPath> or sb://<namespace>.servicebus.windows.net/<entityPath>; that is, http://contoso.servicebus.windows.net/contosoTopics/T1/Subscriptions/S3.

Generate Sas Key Service Bus

The expiry is represented as the number of seconds since the epoch 00:00:00 UTC on 1 January 1970.

The shared access authorization rule used for signing must be configured on the entity specified by this URI, or by one of its hierarchical parents. For example, http://contoso.servicebus.windows.net/contosoTopics/T1 or http://contoso.servicebus.windows.net in the previous example.

A SAS token is valid for all resources under the <resourceURI> used in the string-to-sign.

The KeyName in the SAS token refers to the keyName of the shared access authorization rule used to generate the token.

The URL-encoded-resourceURI must be the same as the URI used in the string-to-sign during the computation of the signature. It should be percent-encoded.

How to use Shared Access Signature authentication with Service Bus

The following scenarios include configuration of authorization rules, generation of SAS tokens, and client authorization.

Azure Storage Generate Sas

For a full working sample of a Service Bus application that illustrates the configuration and uses SAS authorization, see Shared Access Signature authentication with Service Bus. A related sample that illustrates the use of SAS authorization rules configured on namespaces or topics to secure Service Bus subscriptions is available here: Using Shared Access Signature (SAS) authentication with Service Bus Subscriptions.

Sas

Access Shared Access Authorization rules on a namespace

Operations on the Service Bus namespace root require certificate authentication. You must upload a management certificate for your Azure subscription. To upload a management certificate, click Settings in the left-hand pane of the Azure classic portal. For more information about Azure management certificates, see Create a Management Certificate for Azure.

The endpoint for accessing shared access authorization rules on a Service Bus namespace is as follows:

To create a SharedAccessAuthorizationRule object on a Service Bus namespace, execute a POST operation on this endpoint with the rule information serialized as JSON or XML. For example:

Similarly, use a GET operation on the endpoint to read the authorization rules configured on the namespace.

To update or delete a specific authorization rule, use the following endpoint:

Accessing Shared Access Authorization rules on an entity

You can access a Microsoft.ServiceBus.Messaging.SharedAccessAuthorizationRule object configured on a Service Bus queue or topic through the AuthorizationRules collection in the corresponding QueueDescription, TopicDescription, or NotificationHubDescription objects.

The following code shows how to add authorization rules for a queue.

Using Shared Access Signature authorization

Applications using the Azure .NET SDK with the Service Bus .NET libraries can use SAS authorization through the SharedAccessSignatureTokenProvider class. The following code illustrates the use of the token provider to send messages to a Service Bus queue.

Applications can also use SAS for authentication by using a SAS connection string in methods that accept connection strings.

Note that to use SAS authorization with Service Bus relays, you can use SAS keys configured on the Service Bus namespace. If you explicitly create a relay on the namespace (NamespaceManager with a RelayDescription) object, you can set the SAS rules just for that relay. To use SAS authorization with Service Bus subscriptions, you can use SAS keys configured on a Service Bus namespace or on a topic.

Rights required for Service Bus operations

The following table shows the access rights required for various operations on Service Bus resources.

Sas Set Key

OperationClaim RequiredClaim Scope
Namespace
Configure authorization rule on a namespaceManageAny namespace address
Service Registry
Enumerate Private PoliciesManageAny namespace address
Relay
Begin listening on a namespaceListenAny namespace address
Send messages to a listener at a namespaceSendAny namespace address
Queue
Create a queueManageAny namespace address
Delete a queueManageAny valid queue address
Enumerate queuesManage/$Resources/Queues
Get the queue descriptionManage or SendAny valid queue address
Configure authorization rule for a queueManageAny valid queue address
Send into to the queueSendAny valid queue address
Receive messages from a queueListenAny valid queue address
Abandon or complete messages after receiving the message in peek-lock modeListenAny valid queue address
Defer a message for later retrievalListenAny valid queue address
Deadletter a messageListenAny valid queue address
Get the state associated with a message queue sessionListenAny valid queue address
Set the state associated with a message queue sessionListenAny valid queue address
Topic
Create a topicManageAny namespace address
Delete a topicManageAny valid topic address
Enumerate topicsManage/$Resources/Topics
Get the topic descriptionManage or SendAny valid topic address
Configure authorization rule for a topicManageAny valid topic address
Send to the topicSendAny valid topic address
Subscription
Create a subscriptionManageAny namespace address
Delete subscriptionManage../myTopic/Subscriptions/mySubscription
Enumerate subscriptionsManage../myTopic/Subscriptions
Get subscription descriptionManage or Listen../myTopic/Subscriptions/mySubscription
Abandon or complete messages after receiving the message in peek-lock modeListen../myTopic/Subscriptions/mySubscription
Defer a message for later retrievalListen../myTopic/Subscriptions/mySubscription
Deadletter a messageListen../myTopic/Subscriptions/mySubscription
Get the state associated with a topic sessionListen../myTopic/Subscriptions/mySubscription
Set the state associated with a topic sessionListen../myTopic/Subscriptions/mySubscription
Rule
Create a ruleManage../myTopic/Subscriptions/mySubscription
Delete a ruleManage../myTopic/Subscriptions/mySubscription
Enumerate rulesManage or Listen../myTopic/Subscriptions/mySubscription/Rules
Notification Hubs
Create a notification hubManageAny namespace address
Create or update registration for an active deviceListen or Manage../notificationHub/tags/{tag}/registrations
Update PNS informationListen or Manage../notificationHub/tags/{tag}/registrations/updatepnshandle
Send to a notification hubSend../notificationHub/messages

Next steps

For a high-level overview of SAS in Service Bus, see Shared Access Signatures.

See Service Bus Authentication and Authorization for more background on Service Bus authentication.