Skip to content

Instantly share code, notes, and snippets.

@danigian
Last active February 17, 2020 08:51
Show Gist options
  • Save danigian/d6d81d1956241ede578c951a6cd0d747 to your computer and use it in GitHub Desktop.
Save danigian/d6d81d1956241ede578c951a6cd0d747 to your computer and use it in GitHub Desktop.
ServiceBus authentication with Managed Identities
static async Task ServiceBusTest()
{
// Managed identity needs "Azure Service Bus Data Owner" role assigned and scoped to ServiceBus
var tokenProvider = Microsoft.Azure.ServiceBus.Primitives.TokenProvider.CreateManagedIdentityTokenProvider();
TopicClient topicSendClient = new TopicClient($"sb://{sbNamespace}.servicebus.windows.net/", sbEntity, tokenProvider, Microsoft.Azure.ServiceBus.TransportType.Amqp, null);
await topicSendClient.SendAsync(new Message(Encoding.UTF8.GetBytes("Hello world from Managed Identity")));
await topicSendClient.CloseAsync();
Console.WriteLine($"Message sent to {sbNamespace}");
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.ServiceBus" Version="4.1.1" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment