AuthenticationAzure
Azure service principals: secrets, certificates and federation
A service principal is an identity created for applications, hosted services and automated tools. It signs in with a client secret, a certificate or a federated credential, and Microsoft's guidance puts the secret last.

App registration and service principal
Registering an application in Microsoft Entra ID creates two objects in your home tenant.
- The application object is the global representation of the app. It resides in the home tenant, and credentials are specified on it. The Microsoft Entra admin center lists application objects under App registrations.
- The service principal is the local representation of the app in one tenant. It defines what the app can do in that tenant and what resources it can access. The admin center lists service principals under Enterprise applications.
One application object can have many service principals: one is created in each tenant where the app is used. Microsoft Entra ID has three types of service principal: application, managed identity and legacy.
Create one with a role and scope
az ad sp create-for-rbac --name my-deploy \
--role reader \
--scopes /subscriptions/<subscription-id>/resourceGroups/<resource-group>The output has four keys: appId, displayName, password and tenant. The password cannot be retrieved again, so record it when it is shown. With no --role and --scopes, the command assigns no role, and the new service principal has no access to resources.
Microsoft's warning: when another object already has the same display name, create-for-rbac can change that existing application or service principal.
The three credential types
A client secret is a string value the app uses to identify itself, also called an application password. create-for-rbac creates one by default, valid for one year unless you set --years. Microsoft describes client secrets as less secure than certificates or federated credentials, and says they should not be used in production environments.
A certificate is the credential type Microsoft recommends over a client secret. Only the public certificate is uploaded. The tool that signs in needs access to the private key.
az ad sp create-for-rbac --name my-deploy \
--role reader \
--scopes /subscriptions/<subscription-id>/resourceGroups/<resource-group> \
--create-cert
az login --service-principal --username <app-id> \
--certificate /path/to/cert.pem --tenant <tenant-id>--create-cert creates a self-signed certificate. Unless you store it in Key Vault, the output names the local file that holds the certificate and its private key, under fileWithCertAndPrivateKey. With --keyvault, which also requires --cert, the certificate is created in or retrieved from Azure Key Vault. For production, Microsoft's guidance is a certificate signed by a well-known certificate authority, with Key Vault managing access and lifetime.
A federated credential needs no stored secret. The app registration trusts tokens from an external identity provider, such as GitHub, a Kubernetes cluster, Google Cloud or AWS. The workload exchanges that token for an access token from the Microsoft identity platform. The issuer, subject and audience of the credential must match the values in the token, and the match is case-sensitive.
The Azure CLI reference documents this command for a service principal with no password credential:
az ad sp create-for-rbac --create-password falseA federated credential is then added with az ad app federated-credential create --id <app-id> --parameters credential.json.
Secret expiry
In the Microsoft Entra admin center, the lifetime of a client secret is limited to 24 months, and Microsoft recommends an expiration of less than 12 months. Microsoft's federation page names what expiry costs: the risk of service downtime when credentials expire.
List the credential metadata of an application. The first command lists password credentials and the second lists certificate credentials:
az ad app credential list --id <app-id>
az ad app credential list --id <app-id> --certTo rotate a certificate, append the new one, move the workload to it, then delete the old credential by its key ID. Both commands are from the Azure CLI reference:
az ad app credential reset --id <app-id> --cert "@~/cert.pem" --append
az ad app credential delete --id <app-id> --key-id <key-id>Keep --append in the first command. By default, az ad app credential reset clears all passwords and keys and generates a new password credential. The same command takes --years and --end-date to set the expiry.
When another identity fits better
Microsoft's security guidance for app registrations gives an order of preference:
- A managed identity. Microsoft says to consider one in place of an app registration when the service runs in Azure, does not sign in users, is not a web API and does not operate in multiple tenants. Managed identities require no ongoing credential management.
- A managed identity as the credential of an app registration. Microsoft strongly recommends this whenever possible, and notes it may only be possible when the service runs on Azure.
- A federated credential from another platform. A GitHub Actions workflow is Microsoft's example. Configure federated credentials only from platforms you trust: the app can be no more secure than the identity platform set up as its credential.
- A certificate, when neither a managed identity nor a secure external identity provider is possible.
The same guidance says not to use password credentials. A federated identity credential can be configured on an app registration or on a user-assigned managed identity.
What to check on existing principals
- Credentials. Review each application's credentials for use and expiration. Microsoft warns that a credential left unused on an application can lead to a security breach.
- Sharing. Do not share credentials across applications, and do not keep many credentials on one application.
- Roles. Use the principle of least privilege. Microsoft's example: do not give a service principal
contributoron a subscription if it only needs storage within one resource group. - Lifetime policy. Application management policies can limit the lifetimes of secrets or block their use altogether.
Sources
- Application and service principal objects in Microsoft Entra ID
- Create an Azure service principal with Azure CLI
- Use an Azure service principal with certificate-based authentication
- az ad sp
- az ad app credential
- Add and manage application credentials in Microsoft Entra ID
- Security best practices for application properties in Microsoft Entra ID
- Workload identity federation concepts
Want a second pair of eyes on yours? We do this work for a living.
More guides

Azure managed identities: system-assigned or user-assigned
Authentication · 4 min read
When to pick each type, how to grant a role, how code gets a token and which limits to plan for.

Set up OIDC between GitHub Actions and Azure
Authentication · 2 min read
Five steps from an app registration to a workflow that logs in with no stored secret.

Deploy keys, SSH keys and machine users on GitHub
Authentication · 4 min read
The credentials a server can use to reach a repository, what each one covers, and how to choose.