Authenticate .NET apps to Azure services during local development using developer accounts
When creating cloud applications, developers need to debug and test applications on their local workstation. When an application is run on a developer's workstation during local development, it still must authenticate to any Azure services used by the app. This article covers how to use a developer's Azure credentials to authenticate the app to Azure during local development.
For an app to authenticate to Azure during local development using the developer's Azure credentials, the developer must be signed-in to Azure from the VS Code Azure Tools extension, the Azure CLI, or Azure PowerShell. The Azure SDK for .NET is able to detect that the developer is signed-in from one of these tools and then obtain the necessary credentials from the credentials cache to authenticate the app to Azure as the signed-in user.
This approach is easiest to set up for a development team since it takes advantage of the developers' existing Azure accounts. However, a developer's account will likely have more permissions than required by the application, therefore exceeding the permissions the app will run with in production. As an alternative, you can create application service principals to use during local development which can be scoped to have only the access needed by the app.
1 - Create Azure AD group for local development
Since there are almost always multiple developers who work on an application, it's recommended to first create an Azure AD group to encapsulate the roles (permissions) the app needs in local development. This offers the following advantages.
- Every developer is assured to have the same roles assigned since roles are assigned at the group level.
- If a new role is needed for the app, it only needs to be added to the Azure AD group for the app.
- If a new developer joins the team, they simply must be added to the correct Azure AD group to get the correct permissions to work on the app.
If you have an existing Azure AD group for your development team, you can use that group. Otherwise, complete the following steps to create an Azure AD group.
2 - Assign roles to the Azure AD group
Next, you need to determine what roles (permissions) your app needs on what resources and assign those roles to your app. In this example, the roles will be assigned to the Azure Active Directory group created in step 1. Roles can be assigned a role at a resource, resource group, or subscription scope. This example will show how to assign roles at the resource group scope since most applications group all their Azure resources into a single resource group.
3 - Sign-in to Azure using .NET Tooling
Next you need to sign in to Azure using one of several .NET tooling options. The account you sign into should also exist in the Azure Active Directory group you created and configured earlier.
On the top menu of Visual Studio, navigate to Tools > Options to open the options dialog. In the search bar in the upper left, type Azure to filter the options. Under the Azure Service Authentication, choose Account Selection.
Select the drop-down menu under Choose an account and choose to add a Microsoft Account. A window will open prompting you to pick an account. Enter the credentials for your desired Azure account, and then select the confirmation.
4 - Implement DefaultAzureCredential in your application
DefaultAzureCredential
supports multiple authentication methods and determines the authentication method being used at runtime. In this way, your app can use different authentication methods in different environments without implementing environment specific code.
The order and locations in which DefaultAzureCredential
looks for credentials is found at DefaultAzureCredential.
To implement DefaultAzureCredential
, first add the Azure.Identity
and optionally the Microsoft.Extensions.Azure
packages to your application. You can do this using either the command line or the NuGet Package Manager.
Open a terminal environment of your choice in the application project directory and enter the command below.
dotnet add package Azure.Identity
dotnet add package Microsoft.Extensions.Azure
Azure services are generally accessed using corresponding client classes from the SDK. These classes and your own custom services should be registered in the Program.cs
file so they can be accessed via dependency injection throughout your app. Inside of Program.cs
, follow the steps below to correctly setup your service and DefaultAzureCredential
.
- Include the
Azure.Identity
andMicrosoft.Extensions.Azure
namespaces with a using statement. - Register the Azure service using relevant helper methods.
- Pass an instance of the
DefaultAzureCredential
object to theUseCredential
method.
An example of this is shown in the following code segment.
using Microsoft.Extensions.Azure;
using Azure.Identity;
// Inside of Program.cs
builder.Services.AddAzureClients(x =>
{
x.AddBlobServiceClient(new Uri("https://<account-name>.blob.core.windows.net"));
x.UseCredential(new DefaultAzureCredential());
});
Alternatively, you can also utilize DefaultAzureCredential
in your services more directly without the help of additional Azure registration methods, as seen below.
using Azure.Identity;
// Inside of Program.cs
builder.Services.AddSingleton<BlobServiceClient>(x =>
new BlobServiceClient(
new Uri("https://<account-name>.blob.core.windows.net"),
new DefaultAzureCredential()));
When the above code is run on your local workstation during local development, it will look in the environment variables for an application service principal or at Visual Studio, VS Code, the Azure CLI, or Azure PowerShell for a set of developer credentials, either of which can be used to authenticate the app to Azure resources during local development.
When deployed to Azure this same code can also authenticate your app to other Azure resources. DefaultAzureCredential
can retrieve environment settings and managed identity configurations to authenticate to other services automatically.
Feedback
Submit and view feedback for