Skip to content

Managed Identity with .NET

Managed Identity provides a secure way to authenticate your .NET application without managing connection strings.

This recipe walks these steps in order:

flowchart TD
    START["Managed Identity with .NET"]
    N1["1. Add Azure.Identity NuGet Package"]
    N2["2. Initialize Client with DefaultAzureCredential"]
    N3["3. Dependency Injection Integration"]
    N4["4. RBAC Roles"]
    START --> N1
    N1 --> N2
    N2 --> N3
    N3 --> N4

1. Add Azure.Identity NuGet Package

dotnet add package Azure.Identity

2. Initialize Client with DefaultAzureCredential

Instead of a connection string, provide the resource endpoint and a DefaultAzureCredential instance.

using Azure.Identity;
using Azure.Communication.Sms;

string endpoint = "https://<your-resource-name>.communication.azure.com";

var smsClient = new SmsClient(
    new Uri(endpoint), 
    new DefaultAzureCredential());

// Use the client as usual
await smsClient.SendAsync(...);

3. Dependency Injection Integration

In an ASP.NET Core application, register the client in Program.cs.

using Microsoft.Extensions.Azure;

builder.Services.AddAzureClients(clientBuilder =>
{
    clientBuilder.AddSmsClient(new Uri(builder.Configuration["AcsEndpoint"]));
    clientBuilder.UseCredential(new DefaultAzureCredential());
});

4. RBAC Roles

Ensure your application's identity has the correct roles assigned:

  • Communication User: Access to identity and messaging.
  • Communication Service Contributor: Full management access.

See Also

Sources