Skip to content

Commit 00fc346

Browse files
authored
Merge pull request #177307 from psignoret/patch-15
Add details for how to use PowerShell to grant consent on behalf of one user
2 parents 6df9167 + 8a7127d commit 00fc346

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed

articles/active-directory/manage-apps/manage-consent-requests.md

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ See [Grant tenant-wide admin consent to an application](grant-admin-consent.md)
9595

9696
### Granting consent on behalf of a specific user
9797

98-
Instead of granting consent for the entire organization, an administrator can also use the [Microsoft Graph API](/graph/use-the-api) to grant consent to delegated permissions on behalf of a single user. For more information, see [Get access on behalf of a user](/graph/auth-v2-user).
98+
Instead of granting consent for the entire organization, an administrator can also use the [Microsoft Graph API](/graph/use-the-api) to grant consent to delegated permissions on behalf of a single user. For a detailed example using Microsoft Graph PowerShell, see [Grant consent on behalf of a single user using PowerShell](#grant-consent-on-behalf-of-a-single-user-using-powershell).
9999

100100
## Limiting user access to applications
101101

@@ -116,6 +116,88 @@ To disable all future user consent operations in your entire directory, follow t
116116
:::image type="content" source="media/manage-consent-requests/disable-user-consent-operations.png" alt-text="disabling user consent operations for all apps.":::
117117
5. Disable all future user consent operations by setting the **Users can consent to apps accessing company data on their behalf** toggle to **No** and click the **Save** button.
118118

119+
## Grant consent on behalf of a single user using PowerShell
120+
121+
When a user grants consent on behalf of themselves, the following happens:
122+
123+
1. A service principal for the client application is created, if does not already exist. A service principal is the instance of an application or a service, in your Azure AD tenant. Access granted to the app or service is associated with this service principal object.
124+
1. For each API to which the application requires access, a delegated permission grant is created for the permissions needed by the application to that API, for access on behalf of the user. A delegated permission grant authorizes an application to access an API on behalf of a user, when that user has signed in.
125+
1. The user is assigned the client application. Assigning the application to the user ensures the application is listed in the My Apps page for that user, allowing them to review and revoke the access granted an their behalf.
126+
127+
To manually perform the steps which are equivalent to granting consent to an application on behalf of one user, you will need the following details:
128+
129+
* The app ID for app for which you are granting consent (we'll call this the "client application").
130+
* The API permissions required by the client application. You will need to know the app ID of the API and the permission IDs or claim values.
131+
* The username or object ID for the user on behalf of who access will be granted.
132+
133+
In the following example, we will use [Microsoft Graph PowerShell](/graph/powershell/get-started) to perform the three steps listed above to grant consent on behalf of a single user. For this example, the client application will be [Microsoft Graph Explorer](https://aka.ms/ge), and we will be granting access to the Microsoft Graph API.
134+
135+
```powershell
136+
# The app for which consent is being granted. In this example, we're granting access
137+
# to Microsoft Graph Explorer, an application published by Microsoft.
138+
$clientAppId = "de8bc8b5-d9f9-48b1-a8ad-b748da725064" # Microsoft Graph Explorer
139+
140+
# The API to which access will be granted. Microsoft Graph Explorer makes API
141+
# requests to the Microsoft Graph API, so we'll use that here.
142+
$resourceAppId = "00000003-0000-0000-c000-000000000000" # Microsoft Graph API
143+
144+
# The permissions to grant. Here we're including "openid", "profile", "User.Read"
145+
# and "offline_access" (for basic sign-in), as well as "User.ReadBasic.All" (for
146+
# reading other users' basic profile).
147+
$permissions = @("openid", "profile", "offline_access", "User.Read", "User.ReadBasic.All")
148+
149+
# The user on behalf of who access will be granted. The app will be able to access
150+
# the API on behalf of this user.
151+
$userUpnOrId = "[email protected]"
152+
153+
# Step 0. Connect to Microsoft Graph PowerShell. We need User.ReadBasic.All to get
154+
# users' IDs, Application.ReadWrite.All to list and create service principals,
155+
# DelegatedPermissionGrant.ReadWrite.All to create delegated permission grants,
156+
# and AppRoleAssignment.ReadWrite.All to assign an app role.
157+
# WARNING: These are high-privilege permissions!
158+
Connect-MgGraph -Scopes ("User.ReadBasic.All Application.ReadWrite.All " `
159+
+ "DelegatedPermissionGrant.ReadWrite.All " `
160+
+ "AppRoleAssignment.ReadWrite.All")
161+
162+
# Step 1. Check if a service principal exists for the client application.
163+
# If one does not exist, create it.
164+
$clientSp = Get-MgServicePrincipal -Filter "appId eq '$($clientAppId)'"
165+
if (-not $clientSp) {
166+
$clientSp = New-MgServicePrincipal -AppId $clientAppId
167+
}
168+
169+
# Step 2. Create a delegated permission grant granting the client app access to the
170+
# API, on behalf of the user. (This example assumes that an existing delegated
171+
# permission grant does not already exist, in which case it would be necessary
172+
# to update the existing grant, rather than create a new one.)
173+
$user = Get-MgUser -UserId $userUpnOrId
174+
$resourceSp = Get-MgServicePrincipal -Filter "appId eq '$($resourceAppId)'"
175+
$scopeToGrant = $permissions -join " "
176+
$grant = New-MgOauth2PermissionGrant -ResourceId $resourceSp.Id `
177+
-Scope $scopeToGrant `
178+
-ClientId $clientSp.Id `
179+
-ConsentType "Principal" `
180+
-PrincipalId $user.Id
181+
182+
# Step 3. Assign the app to the user. This ensure the user can sign in if assignment
183+
# is required, and ensures the app shows up under the user's My Apps.
184+
if ($clientSp.AppRoles | ? { $_.AllowedMemberTypes -contains "User" }) {
185+
Write-Warning ("A default app role assignment cannot be created because the " `
186+
+ "client application exposes user-assignable app roles. You must " `
187+
+ "assign the user a specific app role for the app to be listed " `
188+
+ "in the user's My Apps access panel.")
189+
} else {
190+
# The app role ID 00000000-0000-0000-0000-000000000000 is the default app role
191+
# indicating that the app is assigned to the user, but not for any specific
192+
# app role.
193+
$assignment = New-MgServicePrincipalAppRoleAssignedTo `
194+
-ServicePrincipalId $clientSp.Id `
195+
-ResourceId $clientSp.Id `
196+
-PrincipalId $user.Id `
197+
-AppRoleId "00000000-0000-0000-0000-000000000000"
198+
}
199+
```
200+
119201
## Next steps
120202

121203
* [Five steps to securing your identity infrastructure](../../security/fundamentals/steps-secure-identity.md#before-you-begin-protect-privileged-accounts-with-mfa)

0 commit comments

Comments
 (0)