You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Managed identities for Azure resources with Service Bus
3
-
description: This article describes how to use managed identities to access with Azure Service Bus entities (queues, topics, and subscriptions).
4
-
ms.topic: article
2
+
title: Use Managed Identities with Azure Service Bus
3
+
description: Learn how to authenticate and access Azure Service Bus queues, topics, and subscriptions using managed identities for Azure resources.
4
+
ms.topic: how-to
5
5
ms.date: 02/11/2025
6
+
7
+
#customer intent: As a developer, I want to use managed identities to authenticate my application to Azure Service Bus so that I can avoid storing credentials in my code.
8
+
6
9
---
7
10
8
-
# Authenticate a managed identity with Microsoft Entra ID to access Azure Service Bus resources
9
-
Managed identities for Azure resources provide Azure services with an automatically managed identity in Microsoft Entra ID. You can use this identity to authenticate to any service such as Azure Service Bus that supports Microsoft Entra authentication, without having credentials in your code. If you aren't familiar with managed identities, see [Managed identities for Azure resources](../active-directory/managed-identities-azure-resources/overview.md) before proceeding to read through this article.
11
+
# How to use managed identities with Azure Service Bus
12
+
13
+
Managed identities for Azure resources provide Azure services with an automatically managed identity in Microsoft Entra ID. You can use this identity to authenticate to Azure Service Bus without storing credentials in your code.
10
14
11
-
Here are the high-level steps to use a managed identity to access a Service Bus entity:
15
+
This article shows you how to:
12
16
13
-
1. Enable managed identity for your client app or environment. For example, enable managed identity for your Azure App Service app, Azure Functions app, or a virtual machine in which your app is running. Here are the articles that help you with this step:
14
-
-[Configure managed identities for App Service and Azure Functions](../app-service/overview-managed-identity.md)
15
-
-[Configure managed identities for Azure resources on a virtual machine (VM)](../active-directory/managed-identities-azure-resources/qs-configure-portal-windows-vm.md)
16
-
1. Assign Azure Service Bus Data Owner, Azure Service Bus Data Sender, or Azure Service Bus Data Receiver role to the managed identity at the appropriate scope (Azure subscription, resource group, Service Bus namespace, or Service Bus queue or topic). For instructions to assign a role to a managed identity, see [Assign Azure roles using the Azure portal](/azure/role-based-access-control/role-assignments-portal).
17
-
1. In your application, use the managed identity and the endpoint to Service Bus namespace to connect to the namespace.
17
+
> [!div class="checklist"]
18
+
> - Enable a managed identity for your Azure compute resource
19
+
> - Assign Service Bus roles to the managed identity
20
+
> - Connect to Service Bus from your application using the managed identity
18
21
19
-
For example, in .NET, you use the [ServiceBusClient](/dotnet/api/azure.messaging.servicebus.servicebusclient.-ctor#azure-messaging-servicebus-servicebusclient-ctor(system-string-azure-core-tokencredential)) constructor that takes `TokenCredential` and `fullyQualifiedNamespace` (a string, for example: `cotosons.servicebus.windows.net`) parameters to connect to Service Bus using the managed identity. You pass in [DefaultAzureCredential](/dotnet/api/azure.identity.defaultazurecredential), which derives from `TokenCredential` and uses the managed identity. In `DefaultAzureCredentialOptions`, set the `ManagedIdentityClientId` to the ID of client's managed identity.
22
+
If you're not familiar with managed identities, see [Managed identities for Azure resources](../active-directory/managed-identities-azure-resources/overview.md).
- An Azure subscription. If you don't have one, create a [free account](https://azure.microsoft.com/free/) before you begin.
29
+
- An Azure Service Bus namespace. To create one, see [Create a Service Bus namespace](service-bus-create-namespace-portal.md).
30
+
- A managed identity enabled on your Azure compute resource. See:
31
+
-[Configure managed identities for App Service and Azure Functions](../app-service/overview-managed-identity.md)
32
+
-[Configure managed identities for Azure resources on a virtual machine (VM)](../active-directory/managed-identities-azure-resources/qs-configure-portal-windows-vm.md)
33
33
34
-
> [!IMPORTANT]
35
-
>YoucandisablelocalorSASkeyauthenticationfor a Service Bus namespace and allow only Microsoft Entra authentication. For step-by-step instructions, see [Disable local authentication](disable-local-authentication.md).
34
+
> [!IMPORTANT]
35
+
> You can disable local or SAS key authentication for a Service Bus namespace and allow only Microsoft Entra authentication. For step-by-step instructions, see [Disable local authentication](disable-local-authentication.md).
36
36
37
-
## Azure built-in roles for Azure Service Bus
38
-
MicrosoftEntraauthorizesaccesstosecuredresourcesthrough [Azurerole-basedaccesscontrol (RBAC)](../role-based-access-control/overview.md). AzureServiceBusdefinesasetofAzurebuilt-inrolesthatencompasscommonsetsofpermissionsusedtoaccessServiceBusentities. Youcanalsodefinecustomrolesfor accessing the data.
37
+
## Assign a Service Bus role to the managed identity
38
+
39
+
Microsoft Entra authorizes access to secured resources through [Azure role-based access control (RBAC)](../role-based-access-control/overview.md). Azure Service Bus provides Azure built-in roles that encompass common sets of permissions used to access Service Bus entities. You can also define custom roles.
40
+
41
+
The following table lists the Azure built-in roles for authorizing access to a Service Bus namespace:
42
+
43
+
| Role | Description |
44
+
|------|-------------|
45
+
|[Azure Service Bus Data Owner](../role-based-access-control/built-in-roles.md#azure-service-bus-data-owner)| Full access to Service Bus namespace and its entities (queues, topics, subscriptions, and filters) |
46
+
|[Azure Service Bus Data Sender](../role-based-access-control/built-in-roles.md#azure-service-bus-data-sender)| Send messages to Service Bus queues and topics |
47
+
|[Azure Service Bus Data Receiver](../role-based-access-control/built-in-roles.md#azure-service-bus-data-receiver)| Receive messages from Service Bus queues and subscriptions |
39
48
40
-
Azure provides the following Azure built-in roles for authorizing access to a Service Bus namespace:
49
+
### Assign a role in the Azure portal
41
50
42
-
- [Azure Service Bus Data Owner](../role-based-access-control/built-in-roles.md#azure-service-bus-data-owner):UsethisroletoallowfullaccesstoServiceBusnamespaceanditsentities (queues, topics, subscriptions, andfilters)
The Azure portal doesn't support assigning managed identities to Service Bus roles at the topic subscription level. Use the Azure CLI [azroleassignmentcreate](/cli/azure/role/assignment#az-role-assignment-create)command to assign a role at any scope:
62
80
63
81
```azurecli
64
82
az role assignment create \
@@ -70,24 +88,26 @@ az role assignment create \
70
88
For more information about how built-in roles are defined, see [Understand role definitions](../role-based-access-control/role-definitions.md#control-and-data-actions). For information about creating Azure custom roles, see [Azure custom roles](../role-based-access-control/custom-roles.md).
> If the source service or app doesn't restart after the access to a Service Bus entity is disabled by removing the source's managed identity from the Service Bus RBAC role, the source app might continue to send/receive messages to/from the Service Bus entity until the token expires (default token validity is 24 hours). This behavior is by design.
74
92
>
75
93
> Therefore, after you remove the source's managed identity from the RBAC role, restart the source app or service to immediately expire the token and prevent it from sending messages to or receiving messages from the Service Bus entity.
76
94
77
-
## UsingSDKs
95
+
## Connect to Service Bus using managed identity in Azure SDKs
96
+
97
+
Azure SDKs for .NET, Java, JavaScript, and Python support managed identity authentication with Service Bus. The following example shows how to connect using the .NET SDK.
78
98
79
99
In .NET, the [ServiceBusClient](/dotnet/api/azure.messaging.servicebus.servicebusclient) object is initialized by using a constructor that takes a fully qualified namespace and a `TokenCredential`. The `DefaultAzureCredential` derives from `TokenCredential`, which automatically uses the managed identity configured for the app. The flow of the managed identity context to Service Bus and the authorization handshake are automatically handled by the token credential. It's a simpler model than using SAS.
You send and receive messages as usual using [ServiceBusSender](/dotnet/api/azure.messaging.servicebus.servicebussender) and [ServiceBusReceiver](/dotnet/api/azure.messaging.servicebus.servicebusreceiver) or [ServiceBusProcessor](/dotnet/api/azure.messaging.servicebus.servicebusprocessor).
86
106
87
107
For complete step-by-step instructions to send and receive messages using a managed identity, see the following quickstarts. These quickstarts have the code to use a service principal to send and receive messages, but the code is the same for using a managed identity.
@@ -96,4 +116,7 @@ For complete step-by-step instructions to send and receive messages using a mana
96
116
97
117
98
118
## Next steps
99
-
See [this .NET web application sample on GitHub](https://github.com/Azure-Samples/app-service-msi-servicebus-dotnet/tree/master), which uses a managed identity to connect to Service Bus to send and receive messages. Add the identity of the app service to the **Azure Service Bus Data Owner** role.
119
+
120
+
-[Sample: .NET web application using managed identity with Service Bus](https://github.com/Azure-Samples/app-service-msi-servicebus-dotnet/tree/master)
121
+
-[What are managed identities for Azure resources?](../active-directory/managed-identities-azure-resources/overview.md)
122
+
-[Disable local authentication for Service Bus](disable-local-authentication.md)
0 commit comments