Choosing Service Principals and Managed Identities
This lesson explains workload identities, and how to choose the correct type.
Learning outcomes
After completing this lesson, you will be able to:
- Explain why automation needs its own identity.
- Describe service principals and their credentials.
- Distinguish system-assigned from user-assigned identities.
- Choose an identity from stated requirements.
Why automation needs its own identity
A pipeline must never run as a person. A personal account may hold far more access than the pipeline needs, and it is disabled when that person leaves. It also makes audit logs misleading. A workload identity is an identity for software, and Microsoft Entra ID offers two kinds: service principals and managed identities.
Service principals
Registering an application in Microsoft Entra ID creates an application object, which is the global definition, and a service principal, which is its local identity in a tenant. Azure roles are assigned to the service principal. To sign in, it needs a credential, which may be a client secret, a certificate or a federated credential.
Secrets are the weakest option: they expire, they must be stored somewhere, and they can leak. Certificates are stronger, but they still need management. A federated credential stores no secret at all, and the lesson on secretless authentication explains it. A service principal can be used from anywhere, which suits workloads running outside Azure, including hosted pipeline agents.
Service principal credentials from weakest to strongest
Managed identities
A managed identity is an identity that Azure manages, with no credentials for you to handle. A workload on an Azure resource requests a token from a local endpoint, and Azure rotates the underlying credentials automatically. Managed identities have two types.
System-assigned
A system-assigned managed identity is enabled on one Azure resource and shares its lifecycle. Deleting the resource deletes the identity, and it cannot be shared. It suits a single resource that needs its own permissions, and it leaves nothing behind.
User-assigned
A user-assigned managed identity is a standalone Azure resource, which can be attached to many resources. It survives when those resources are deleted. This suits several resources that need the same access, and resources that are replaced often, such as those in a scale set, because role assignments can be made in advance and they stay in place. A user-assigned identity can also hold federated credentials, so a pipeline can use it, even though the pipeline does not run on an Azure resource.
System-assigned or user-assigned?
Choosing an identity
The following comparison sets out the differences. The main question is where the workload runs.
On a self-hosted agent running on an Azure virtual machine, a managed identity assigned to that machine can be used, and the agent needs no stored credential. Note that all pipelines using that agent then share the identity, so the agent pool must be protected. Microsoft-hosted agents cannot use your managed identities directly, which is why federation exists.
The following diagram shows one user-assigned managed identity shared by twelve container apps, with a single role assignment.

Choosing a workload identity
Applying least privilege
Whichever identity is selected, role assignments should be as narrow as possible. Assign roles at the resource group or resource level, rather than the subscription, and use specific roles such as Website Contributor rather than Contributor when these are enough. Separate identities for each environment limit the damage if one is misused, and they stop a pull request pipeline from reaching production.
Least privilege for workload identities
Choosing the type
Twelve container apps must read secrets from the same key vault. Apps are often deleted and recreated, and the security team wants to assign the vault role once, without repeating it. Which identity should you use?
Use one user-assigned managed identity, attached to all the apps, with the Key Vault Secrets User role on the vault. The role survives when an app is recreated. System-assigned identities would each need their own role assignment after each recreation. A service principal with a secret would add a credential to store and rotate.
Why can a Microsoft-hosted agent not use your system-assigned managed identity?
Show answer
The agent is not one of your Azure resources, so it cannot hold that identity. Federation solves this.
Exam Tips
Remember for the exam
- A system-assigned identity shares its resource's lifecycle.
- A user-assigned identity is reusable and independent of any resource.
- Use a service principal for workloads outside Azure.
- Federated credentials can sit on app registrations or user-assigned identities.
- Use a separate identity per environment, with narrowly scoped roles.