Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This article shows you how to connect to Azure Event Hubs from an application that runs on an Azure virtual machine (VM) by using a managed identity. When you use a managed identity, your application authenticates to Event Hubs without storing connection strings or access keys in code or configuration. Azure provisions and rotates the credentials automatically.
In this article, you complete the following tasks:
- Enable a system-assigned managed identity on a VM.
- Grant the managed identity permission to send events to an event hub.
- Create and run a sample application on the VM that uses the managed identity.
To learn how managed identity authentication works with Event Hubs, see Managed identity authentication for Azure Event Hubs.
Prerequisites
- An Azure account with an active subscription. If you don't have one, create a free account.
- An Event Hubs namespace and an event hub. To create them, see Quickstart: Create an event hub using Azure portal.
- An Azure virtual machine. To create one, see Quickstart: Create a Windows VM in the Azure portal or Quickstart: Create a Linux VM in the Azure portal.
- Permission to assign Azure roles at the target scope. The Owner or User Access Administrator role includes this permission.
- The .NET SDK installed on the VM to build and run the sample application.
Enable a managed identity on the VM
Enable a system-assigned managed identity on the VM. Azure creates an identity in Microsoft Entra ID that's tied to the lifecycle of the VM.
Sign in to the Azure portal.
Go to your virtual machine.
On the VM page, select Security > Identity from the left menu.
On the Identity page, confirm that you're on the System assigned tab.
For the Status field, select On.
Select Save on the command bar, and then select Yes to confirm.
After Azure creates the identity, the Object (principal) ID appears on the page. You use this identity in the next section.
To enable a user-assigned managed identity instead, see Configure managed identities for Azure resources on a VM. A user-assigned identity is a standalone resource that you can assign to more than one VM.
Grant the managed identity access to Event Hubs
Assign an Azure built-in role to the managed identity so that it can send events to your event hub. This example assigns the Azure Event Hubs Data Sender role at the scope of a single event hub. Grant the narrowest scope that meets your application's needs.
- In the Azure portal, go to your event hub.
- On the event hub page, select Access control (IAM) from the left menu.
- Select Add > Add role assignment.
- On the Role tab, select Azure Event Hubs Data Sender, and then select Next.
- On the Members tab, for Assign access to, select Managed identity.
- Select + Select members, choose the Virtual machine managed identity that you enabled in the previous section, and then select Select.
- Select Review + assign to complete the role assignment.
For the full list of roles and scopes, see Azure built-in roles for Azure Event Hubs. To assign roles in the Azure portal, see Assign Azure roles using the Azure portal.
Note
Role assignments can take a few minutes to propagate. If your application can't authenticate immediately, wait and try again.
Create and run the sample application on the VM
Create a .NET console application that sends events to your event hub. When the application runs on the VM, DefaultAzureCredential automatically detects the managed identity, so you don't provide a connection string or key.
Connect to the VM. For a Windows VM, use Remote Desktop. For a Linux VM, use SSH.
In a terminal on the VM, create a project and add the required packages:
dotnet new console --name EventHubsManagedIdentitySample cd EventHubsManagedIdentitySample dotnet add package Azure.Messaging.EventHubs dotnet add package Azure.IdentityReplace the contents of
Program.cswith the following code. Replace<namespace>with your Event Hubs namespace name and<event-hub-name>with your event hub name.using Azure.Identity; using Azure.Messaging.EventHubs; using Azure.Messaging.EventHubs.Producer; // DefaultAzureCredential detects the managed identity available on the VM. var credential = new DefaultAzureCredential(); var producerClient = new EventHubProducerClient( "<namespace>.servicebus.windows.net", "<event-hub-name>", credential); using EventDataBatch eventBatch = await producerClient.CreateBatchAsync(); eventBatch.TryAdd(new EventData("First event from a managed identity")); eventBatch.TryAdd(new EventData("Second event from a managed identity")); await producerClient.SendAsync(eventBatch); await producerClient.DisposeAsync(); Console.WriteLine("Events sent to the event hub.");Build and run the application on the VM:
dotnet runThe application uses the VM's managed identity to authenticate and sends the events. When the events are sent, the console displays
Events sent to the event hub.To confirm that the events arrived, use the Data Explorer in the Azure portal for your event hub, or the metrics on the namespace Overview page.
Tip
When you use a user-assigned managed identity, or when more than one identity is available on the VM, set the client ID so that DefaultAzureCredential selects the correct identity. For more information, see DefaultAzureCredentialOptions.ManagedIdentityClientId.