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.
Use DeveloperToolsCredential for most local
development. This article explains when to test application identity with a
service principal. In the following sections, you learn:
- How to register an application with Microsoft Entra to create a service principal
- How to use Microsoft Entra groups to efficiently manage permissions
- How to assign roles to scope permissions
- How to authenticate using a service principal from your app code
By using dedicated application service principals, you can follow the principle of least privilege when accessing Azure resources. Limit permissions to the specific requirements of the app during development, preventing accidental access to Azure resources intended for other apps or services. This approach also helps avoid issues when the app is moved to production by ensuring it isn't over-privileged in the development environment.
When you register the app in Azure, you create an application service principal. For local development:
- Create a separate app registration for each developer working on the app so each developer has their own application service principal and doesn't need to share credentials.
- Create a separate app registration for each app to limit the app's permissions to only what is necessary.
During local development, set environment variables with the application service principal's identity. The Azure Identity library reads these environment variables to authenticate the app to the required Azure resources.
Register the app in Azure
Application service principal objects are created through an app registration in Azure using either the Azure portal or Azure CLI.
In the Azure portal, use the search bar to navigate to the App registrations page.
On the App registrations page, select + New registration.
On the Register an application page:
- For the Name field, enter a descriptive value that includes the app name and the target environment.
- For the Supported account types, select Accounts in this organizational directory only (Microsoft Customer Led only - Single tenant), or whichever option best fits your requirements.
Select Register to register your app and create the service principal.
On the App registration page for your app, copy the Application (client) ID and Directory (tenant) ID and paste them in a temporary location for later use in your app code configurations.
Select Add a certificate or secret to set up credentials for your app.
On the Certificates & secrets page, select + New client secret.
In the Add a client secret flyout panel that opens:
- For the Description, enter a value of Current.
- For the Expires value, leave the default recommended value of 180 days.
- Select Add to add the secret.
On the Certificates & secrets page, copy the Value property of the client secret for use in a future step.
Note
The client secret value is only displayed once after the app registration is created. You can add more client secrets without invalidating this client secret, but there's no way to display this value again.
Create a Microsoft Entra group for local development
Create a Microsoft Entra group to encapsulate the roles (permissions) the app needs in local development rather than assigning the roles to individual service principal objects. This approach offers the following advantages:
- Every developer has the same roles assigned at the group level.
- If a new role is needed for the app, it only needs to be added to the group for the app.
- If a new developer joins the team, a new application service principal is created for the developer and added to the group, ensuring the developer has the right permissions to work on the app.
Navigate to the Microsoft Entra ID overview page in the Azure portal.
Select All groups from the left-hand menu.
On the Groups page, select New group.
On the New group page, fill out the following form fields:
- Group type: Select Security.
- Group name: Enter a name for the group that includes a reference to the app or environment name.
- Group description: Enter a description that explains the purpose of the group.
Select the No members selected link under Members to add members to the group.
In the flyout panel that opens, search for the service principal you created earlier and select it from the filtered results. Choose the Select button at the bottom of the panel to confirm your selection.
Select Create at the bottom of the New group page to create the group and return to the All groups page. If you don't see the new group listed, wait a moment and refresh the page.
Assign roles to the group
Next, determine what roles (permissions) your app needs on what resources and assign those roles to the Microsoft Entra group you created. Groups can be assigned a role at the resource, resource group, or subscription scope. This example shows how to assign roles at the resource group scope, since most apps group all their Azure resources into a single resource group.
In the Azure portal, navigate to the Overview page of the resource group that contains your app.
Select Access control (IAM) from the left navigation.
On the Access control (IAM) page, select + Add and then choose Add role assignment from the drop-down menu. The Add role assignment page provides several tabs to configure and assign roles.
On the Role tab, use the search box to locate the role you want to assign. Select the role, and then choose Next.
On the Members tab:
- For the Assign access to value, select User, group, or service principal .
- For the Members value, choose + Select members to open the Select members flyout panel.
- Search for the Microsoft Entra group you created earlier and select it from the filtered results. Choose Select to select the group and close the flyout panel.
- Select Review + assign at the bottom of the Members tab.
On the Review + assign tab, select Review + assign at the bottom of the page.
Set the app environment variables
ClientSecretCredential takes the tenant ID, client ID, and client secret as
constructor arguments. Set these values in environment variables and read them
in your app, as shown in the following example.
Set the following environment variables with values you get from the app registration process:
AZURE_CLIENT_ID: Identifies the registered app in Azure.AZURE_TENANT_ID: The ID of the Microsoft Entra tenant.AZURE_CLIENT_SECRET: The secret credential that you generate for the app.
export AZURE_CLIENT_ID=<your-client-id>
export AZURE_TENANT_ID=<your-tenant-id>
export AZURE_CLIENT_SECRET=<your-client-secret>
Authenticate to Azure services from your app
The Azure Identity library provides different TokenCredential implementations for various scenarios and Microsoft Entra authentication flows.
Add the
azure_identitycrate to your project.cargo add azure_identityFor any Rust code that creates an Azure SDK client object, you want to:
- Import the
azure_identitycrate. - Create a
ClientSecretCredentialinstance by using the tenant ID, client ID, and client secret. - Pass the credential instance to the Azure SDK client constructor.
- Import the
The following code segment shows an example using a Key Vault SecretClient:
use azure_identity::ClientSecretCredential;
use azure_core::credentials::Secret;
use azure_security_keyvault_secrets::SecretClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let tenant_id = std::env::var("AZURE_TENANT_ID")?;
let client_id = std::env::var("AZURE_CLIENT_ID")?;
let client_secret = std::env::var("AZURE_CLIENT_SECRET")?;
// Create a credential using the service principal
let credential = ClientSecretCredential::new(
&tenant_id,
client_id,
Secret::new(client_secret),
None,
)?;
// Create a client for the specified Key Vault
let client = SecretClient::new(
"https://<your-key-vault-name>.vault.azure.net/",
credential.clone(),
None,
)?;
// Use the client to interact with Azure Key Vault
// let secret = client.get("secret-name", None).await?;
Ok(())
}