Grant consent on behalf of a single user by using PowerShell
In this article, you learn how to grant consent on behalf of a single user by using PowerShell.
When a user grants consent for themselves, the following events occur more often:
A service principal for the client application is created, if it doesn't already exist. A service principal is the instance of an application or a service in your Microsoft Entra tenant. Access granted to the app or service is associated with this service principal object.
For each API to which the application requires access, a delegated permission grant to that API is created for the permissions that the application needs. The access is granted on behalf of the user. A delegated permission grant authorizes an application to access an API on behalf of a user, when that user signs in.
The user is assigned the client application. Assigning the application to the user ensures that the application is listed in the My Apps portal for that user. The user can review and revoke the access that granted on their behalf from their My Apps portal.
Prerequisites
To grant consent to an application on behalf of one user, you need:
- A user account with a Privileged Role Administrator, Application Administrator, or Cloud Application Administrator
Grant consent on behalf of a single user
Before you start, record the following details from the Microsoft Entra admin center:
- The app ID for the app that you're granting consent. For purposes of this article, we call it the client application.
- The API permissions that the client application requires. Find out the app ID of the API and the permission IDs or claim values.
- The username or object ID for the user on whose behalf access is granted.
For this example, we use Microsoft Graph PowerShell to grant consent on behalf of a single user.
To grant consent to an application on behalf of one user using Microsoft Graph PowerShell, you need to sign in as at least a Cloud Application Administrator.
# The app for which consent is being granted. In this example, we're granting access
# to Microsoft Graph Explorer, an application published by Microsoft.
$clientAppId = "de8bc8b5-d9f9-48b1-a8ad-b748da725064" # Microsoft Graph Explorer
# The API to which access will be granted. Microsoft Graph Explorer makes API
# requests to the Microsoft Graph API, so we'll use that here.
$resourceAppId = "00000003-0000-0000-c000-000000000000" # Microsoft Graph API
# The permissions to grant. Here we're including "openid", "profile", "User.Read"
# and "offline_access" (for basic sign-in), as well as "User.ReadBasic.All" (for
# reading other users' basic profile).
$permissions = @("openid", "profile", "offline_access", "User.Read", "User.ReadBasic.All")
# The user on behalf of whom access will be granted. The app will be able to access
# the API on behalf of this user.
$userUpnOrId = "user@example.com"
# Step 0. Connect to Microsoft Graph PowerShell. We need User.ReadBasic.All to get
# users' IDs, Application.ReadWrite.All to list and create service principals,
# DelegatedPermissionGrant.ReadWrite.All to create delegated permission grants,
# and AppRoleAssignment.ReadWrite.All to assign an app role.
# WARNING: These are high-privilege permissions!
Connect-MgGraph -Environment China -ClientId 'YOUR_CLIENT_ID' -TenantId 'YOUR_TENANT_ID' -Scopes ("User.ReadBasic.All Application.ReadWrite.All " `
+ "DelegatedPermissionGrant.ReadWrite.All " `
+ "AppRoleAssignment.ReadWrite.All")
# Step 1. Check if a service principal exists for the client application.
# If one doesn't exist, create it.
$clientSp = Get-MgServicePrincipal -Filter "appId eq '$($clientAppId)'"
if (-not $clientSp) {
$clientSp = New-MgServicePrincipal -AppId $clientAppId
}
# Step 2. Create a delegated permission that grants the client app access to the
# API, on behalf of the user. (This example assumes that an existing delegated
# permission grant does not already exist, in which case it would be necessary
# to update the existing grant, rather than create a new one.)
$user = Get-MgUser -UserId $userUpnOrId
$resourceSp = Get-MgServicePrincipal -Filter "appId eq '$($resourceAppId)'"
$scopeToGrant = $permissions -join " "
$grant = New-MgOauth2PermissionGrant -ResourceId $resourceSp.Id `
-Scope $scopeToGrant `
-ClientId $clientSp.Id `
-ConsentType "Principal" `
-PrincipalId $user.Id
# Step 3. Assign the app to the user. This ensures that the user can sign in if assignment
# is required, and ensures that the app shows up under the user's My Apps portal.
if ($clientSp.AppRoles | ? { $_.AllowedMemberTypes -contains "User" }) {
Write-Warning ("A default app role assignment cannot be created because the " `
+ "client application exposes user-assignable app roles. You must " `
+ "assign the user a specific app role for the app to be listed " `
+ "in the user's My Apps portal.")
} else {
# The app role ID 00000000-0000-0000-0000-000000000000 is the default app role
# indicating that the app is assigned to the user, but not for any specific
# app role.
$assignment = New-MgServicePrincipalAppRoleAssignedTo `
-ServicePrincipalId $clientSp.Id `
-ResourceId $clientSp.Id `
-PrincipalId $user.Id `
-AppRoleId "00000000-0000-0000-0000-000000000000"
}