MSBA-273 · Lesson 2 of 12 · 21 min

Authentication and app registrations

▶ Video coming soon A video version of this lesson is on the way.

Wiremu's first experiments with the Web API had all run under his own account. He signed in through a browser window, the REST client received a token, and every request ran with his privileges. That was right for exploring and wrong for anything that would run on its own. When Kwabena asked whether the nightly research export could use Wiremu's login until a better option was found, Signe answered before Wiremu could: a person's account would stop working the day that person changed roles, it would carry far more privilege than the job needed, and every change the job made would appear in the audit history as if Wiremu had made it by hand. The second session was about giving each integration an identity of its own.

Microsoft Entra ID and OAuth 2.0

Dataverse does not accept user names and passwords from integration code. Every request carries an access token issued by Microsoft Entra ID, the identity service behind Microsoft 365 and Azure, and Dataverse trusts the tokens Entra ID signs. The protocol is OAuth 2.0, with OpenID Connect for sign-in. The client application asks Entra ID for a token for a specific resource, here the environment's URL; Entra ID checks who is asking and whether they are allowed; and the client then sends the token to Dataverse in the Authorization header as a bearer token.

Access tokens are short-lived, typically about an hour. Client libraries such as the Microsoft Authentication Library, known as MSAL, cache tokens and renew them before they expire, so application code should ask the library for a token each time it needs one rather than storing tokens itself. A token is a credential: anyone who holds it can act as the caller until it expires, so tokens must never be written to logs, files, chat messages or source control.

Two token flows side by side. Left, delegated: a user signs in through the app, Entra ID issues a token for the user, and Dataverse applies the user's security roles. Right, application: a service presents its own credential to Entra ID, receives a token for the application, and Dataverse applies the roles of the matching application user.
Delegated access acts as a person; application access acts as the integration itself.

Delegated and application access

There are two ways an integration can act. With delegated access, a person signs in and the application acts on that person's behalf. Dataverse sees the user, applies the user's security roles and records the user in audit history. A single-page app, a mobile app or a Teams tab that shows people their own data uses delegated access. The application needs the delegated permission for Dataverse, shown in the Entra admin center under the Dynamics CRM API as user_impersonation, and each user must have a license and a role in the environment.

With application access, sometimes called app-only or server-to-server, no person is involved. The application proves its own identity to Entra ID with a credential and receives a token that represents the application. Dataverse matches the token to an application user in the environment and applies that user's security roles. Nightly jobs, message handlers, Azure Functions and anything else that runs unattended should use application access, so that the job's identity, privileges and audit trail belong to the job.

Choosing the access type and flow for common integration callers.
SituationAccess typeOAuth flowIdentity Dataverse sees
A web or single-page app showing a person their own recordsDelegatedAuthorization code with PKCEThe signed-in user
A command-line tool a developer runsDelegatedInteractive or device codeThe developer
A nightly export or sync jobApplicationClient credentialsAn application user
An Azure Function handling messagesApplicationManaged identityAn application user
A GitHub Actions deploymentApplicationWorkload identity federationAn application user

App registrations

An app registration is the record in Entra ID that describes an application. Creating one gives the application a client identifier, also called the application ID, and places it in a tenant with a tenant identifier. The registration holds the settings the protocol needs: redirect addresses for interactive sign-in, the permissions the application requests, and the credentials it can use to prove its identity. When the registration is created, Entra ID also creates a service principal in the tenant, shown as an enterprise application, which is the object that actually signs in and that administrators assign or restrict.

Lumenvale's practice, set by Rupert, was one registration per integration and per purpose, named so that anyone reading the tenant could tell what it was for: lcc-research-export, lcc-ledgerpoint-sync-test and so on. A registration shared by several integrations makes it impossible to rotate a credential or remove access for one of them without breaking the others, and it hides which integration made which change.

Credentials: secrets, certificates and federation

For the client credentials flow, the application must present a credential. There are three kinds. A client secret is a generated string, like a password for the application. It is the easiest to set up and the weakest: it can be copied, it expires and must be rotated, and it is often found later in a configuration file. A certificate credential uses a private key that the application holds and a public key uploaded to the registration; the application signs an assertion with the private key instead of sending a secret. A federated identity credential trusts tokens issued by another identity provider, such as GitHub Actions or Kubernetes, for a specific workload, so that no secret exists at all.

Microsoft's guidance is to prefer certificates or federated credentials over secrets, and to use managed identities wherever the code runs in Azure. Lumenvale's rule followed that guidance: no new client secrets for production, certificates kept in Azure Key Vault where a credential was unavoidable, and federated credentials for the GitHub Actions workflows that deploy solutions. Where an existing integration still used a secret, it lived in Key Vault, never in an app setting or a repository, and its expiry date was on the CoE team's calendar.

Listing 2.1: The client credentials token request with a certificate assertion, shown for understanding; MSAL builds it for you (not executable here).
POST https://login.microsoftonline.com/<tenant id>/oauth2/v2.0/token
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials
&client_id=<application id of lcc-research-export>
&scope=https://lumenvale.crm3.dynamics.com/.default
&client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer
&client_assertion=<assertion signed with the certificate's private key>

The scope in Listing 2.1 is the environment URL followed by /.default, which asks for whatever application permissions the registration holds for that resource. For Dataverse the real authorization happens inside the environment, through the application user's security roles, so the token mainly proves which application is calling.

Application users

A token for an application is not enough on its own. Dataverse only accepts it if the environment contains an application user linked to the registration's application ID. Administrators create application users from the Application users page in the environment's settings in the Power Platform admin center, choosing the app registration, the business unit and one or more security roles. Application users do not consume a user license, cannot sign in interactively, and appear in audit history under their own names.

The security role is where least privilege happens. Signe built a role named lcc Research Export Reader that could read completed work orders, buildings and rooms at organization level and nothing else. The research export's application user held only that role. If the export's credential were ever stolen, the thief could read a subset of facilities data and could change nothing. Giving an application user the System Administrator role because it is quicker is one of the most common integration mistakes, and one of the most expensive.

Application users also need to exist in every environment where the integration runs. Lumenvale created separate registrations for test and production, each with an application user only in its own environment, so that a test job could never write to production by mistake. The deployment pipeline did not create application users; Rupert's team did, as part of the environment request, and recorded them in the CoE inventory.

Listing 2.2: ServiceClient with certificate authentication (C#, not executable here).
// .NET: an unattended job connecting with a certificate from the machine's certificate store.
// The connection string holds no secret; the thumbprint identifies the certificate.
var connection =
    "AuthType=Certificate;" +
    "Url=https://lumenvale.crm3.dynamics.com;" +
    "ClientId=<application id of lcc-research-export>;" +
    "Thumbprint=<certificate thumbprint>";

using var client = new ServiceClient(connection);
if (!client.IsReady) throw new InvalidOperationException(client.LastError);

var who = (WhoAmIResponse)client.Execute(new WhoAmIRequest());
Console.WriteLine($"Connected as application user {who.UserId}");

Managed identities

When integration code runs in Azure, a managed identity removes credentials from the design entirely. Azure creates an identity in Entra ID for the resource, such as a Function App, and the platform supplies tokens to code running on that resource. A system-assigned identity lives and dies with its resource; a user-assigned identity is a separate Azure resource that can be attached to one or more resources and survives when they are rebuilt. The code asks the Azure identity library for a token, and nothing secret is stored anywhere the developer can see.

To let a managed identity call Dataverse, the administrator creates an application user for it, using the identity's client identifier from its enterprise application, and assigns a least-privilege role, exactly as for any other application. The building management alarm design that Signe recorded as decision D4 in November relied on this: the Azure Function that would receive alarms from Service Bus would use a managed identity, so that no credential for Dataverse existed outside Entra ID. Lesson 4 looks at that design in detail; it remains a plan that has not been funded.

Listing 2.3: ServiceClient with a token provider backed by a managed identity (C#, not executable here).
// C# in an Azure Function: a token for Dataverse from the Function App's managed identity.
// DefaultAzureCredential uses the managed identity in Azure and the developer's sign-in locally.
var credential = new DefaultAzureCredential();
var env = "https://lumenvale.crm3.dynamics.com";

using var client = new ServiceClient(
    new Uri(env),
    async _ => (await credential.GetTokenAsync(
        new TokenRequestContext(new[] { env + "/.default" }))).Token);

Managed identity for plug-ins

Plug-ins sometimes need to call Azure resources, for example to read a value from Key Vault or to post to a queue. Dataverse supports managed identity for plug-ins so that plug-in code can acquire tokens without storing a secret. The identity is a user-assigned managed identity or an app registration with a federated identity credential. The administrator creates a managed identity record in Dataverse, binds it to the plug-in assembly or plug-in package, and grants the identity access to the target resource; the assembly or package must be signed. In code, the plug-in obtains the managed identity service from the service provider and calls its AcquireToken method with the scopes it needs. Microsoft's documentation describes several versions of the configuration and recommends the current one, version 2, for new work.

This is different from the Function scenario. A Function uses its managed identity to call Dataverse; a plug-in uses a managed identity to call out of Dataverse to Azure. Wiremu drew both on the whiteboard so that the team would not confuse them.

Token caching and the cost of sign-ins

Asking Entra ID for a new token on every request is slow and can itself be throttled. MSAL and the Azure identity library cache tokens in memory and renew them shortly before expiry. Long-running services should keep one client object for their lifetime rather than creating a new one, and its token cache, for each request.

Worked problem

Token requests with and without caching

A sync job runs every 15 minutes, all day. In the first prototype it created a new client and requested a new token on every run. Tokens last about one hour. How many token requests does the prototype make each day, and roughly how many would a long-running process with a cached token make?

Show solution
  1. Runs per day: 24 hours times 4 runs an hour is 96 runs, so the prototype makes 96 token requests.
  2. With caching in a long-running process, a token is requested about once an hour, so about 24 requests a day.
  3. The cached design makes about a quarter as many requests and removes a sign-in delay from three runs out of four.

Answer: About 96 requests a day without caching and about 24 with a cached token.

Conditional Access and sign-in reviews

Entra ID can apply Conditional Access policies to people and, with the appropriate licensing, to workload identities such as service principals, for example limiting the network locations from which an integration may sign in. Sign-in logs record each application sign-in, including failures. Lumenvale's security team reviewed service principal sign-ins monthly and alerted on any sign-in from an unexpected location. Dr. Philippa Ondaatje-Ruiz, the privacy officer, asked that every application user that could read personal information be listed in the privacy impact assessment for its solution, with the role it held and the person accountable for it.

A checklist for each new integration identity

The identity checklist Rupert added to the integration template.
StepLumenvale practice
Choose the access typeDelegated only when a person is present; otherwise application
Choose the identityManaged identity in Azure; otherwise an app registration
Choose the credentialFederated or certificate; no new client secrets in production
Create the application userOne per environment, in the right business unit
Assign the roleA custom least-privilege role, never System Administrator
Record itOwner, purpose, environment and expiry dates in the CoE inventory
Monitor itSign-in logs reviewed; credential expiry alerts

Common mistakes

  • MistakeRunning an unattended integration under a person's account.

    InsteadUse application access with an application user, or a managed identity.

  • MistakeGiving the application user the System Administrator role.

    InsteadCreate a custom role with only the privileges the integration needs.

  • MistakeStoring a client secret in an app setting or committing it to the repository.

    InsteadPrefer managed identities, federated credentials or certificates; keep any unavoidable secret in Key Vault.

  • MistakeSharing one app registration across several integrations.

    InsteadUse one registration per integration so credentials and access can be managed separately.

  • MistakeRequesting a new token for every API call.

    InsteadLet MSAL or the Azure identity library cache and renew tokens, and reuse one client.

  • MistakeExpecting a valid token to work in an environment with no matching application user.

    InsteadCreate an application user for the registration in each environment.

Summary and key terms

  • Dataverse accepts OAuth 2.0 access tokens issued by Microsoft Entra ID, sent as bearer tokens.
  • Delegated access acts as a signed-in person; application access acts as the integration and suits unattended work.
  • An app registration gives an application its identity; prefer federated credentials or certificates over client secrets.
  • An application user in each environment links the registration to Dataverse and carries a least-privilege security role.
  • Managed identities let code in Azure obtain tokens without stored credentials; plug-ins can use a managed identity to call Azure resources.
  • Token libraries cache and renew tokens; integration identities are inventoried and their sign-ins monitored.
Access token
A short-lived credential issued by Entra ID that a client sends to an API to prove who it is acting as.
Delegated access
Access in which an application acts on behalf of a signed-in user, with that user's permissions.
Client credentials flow
The OAuth flow in which an application obtains a token for itself using its own credential.
App registration
The Entra ID record that defines an application's identity, permissions and credentials.
Application user
A Dataverse user linked to an Entra ID application, used for server-to-server access, with its own security roles.
Managed identity
An Entra ID identity for an Azure resource, whose tokens are supplied by the platform without stored credentials.
Federated identity credential
A trust that lets an application accept tokens from another identity provider for a specific workload instead of a secret.

Practice problems

Work each problem on paper first, then reveal the answer to check.

  1. Which access type should the nightly research export use, and why?

    Show answer

    Application access, because no person is present and the job needs its own identity, privileges and audit trail.

  2. A new registration has a valid certificate but every call returns an error saying the user is not a member of the organization. What is missing?

    Show answer

    An application user for that registration in the environment.

  3. What is the difference between a system-assigned and a user-assigned managed identity?

    Show answer

    A system-assigned identity belongs to one resource and is deleted with it; a user-assigned identity is a separate resource that can be shared and outlives the resources it is attached to.

  4. Why does Lumenvale use separate registrations for test and production?

    Show answer

    So a test job can never act in production and credentials can be managed separately.

Lesson quiz · 5 questions

Score 60% or more to complete this lesson.

Take the quiz