@@ -31,8 +31,7 @@ When a user grants consent on his or her own behalf, the following events occur:
31
31
32
32
To grant consent to an application on behalf of one user, you need:
33
33
34
- - A user account. If you don't already have one, you can [ create an account for free] ( https://azure.microsoft.com/free/?WT.mc_id=A261C142F ) .
35
- - A Global Administrator or Privileged Administrator role.
34
+ - A user account with Global Administrator, Application Administrator, or Cloud Application Administrator
36
35
37
36
## Grant consent on behalf of a single user
38
37
@@ -48,16 +47,20 @@ For this example, we'll use [Microsoft Graph PowerShell](/graph/powershell/get-s
48
47
# The app for which consent is being granted. In this example, we're granting access
49
48
# to Microsoft Graph Explorer, an application published by Microsoft.
50
49
$clientAppId = "de8bc8b5-d9f9-48b1-a8ad-b748da725064" # Microsoft Graph Explorer
50
+
51
51
# The API to which access will be granted. Microsoft Graph Explorer makes API
52
52
# requests to the Microsoft Graph API, so we'll use that here.
53
53
$resourceAppId = "00000003-0000-0000-c000-000000000000" # Microsoft Graph API
54
+
54
55
# The permissions to grant. Here we're including "openid", "profile", "User.Read"
55
56
# and "offline_access" (for basic sign-in), as well as "User.ReadBasic.All" (for
56
57
# reading other users' basic profile).
57
58
$permissions = @("openid", "profile", "offline_access", "User.Read", "User.ReadBasic.All")
59
+
58
60
# The user on behalf of whom access will be granted. The app will be able to access
59
61
# the API on behalf of this user.
60
62
$userUpnOrId = "[email protected] "
63
+
61
64
# Step 0. Connect to Microsoft Graph PowerShell. We need User.ReadBasic.All to get
62
65
# users' IDs, Application.ReadWrite.All to list and create service principals,
63
66
# DelegatedPermissionGrant.ReadWrite.All to create delegated permission grants,
66
69
Connect-MgGraph -Scopes ("User.ReadBasic.All Application.ReadWrite.All " `
67
70
+ "DelegatedPermissionGrant.ReadWrite.All " `
68
71
+ "AppRoleAssignment.ReadWrite.All")
72
+
69
73
# Step 1. Check if a service principal exists for the client application.
70
74
# If one does not exist, create it.
71
75
$clientSp = Get-MgServicePrincipal -Filter "appId eq '$($clientAppId)'"
72
76
if (-not $clientSp) {
73
77
$clientSp = New-MgServicePrincipal -AppId $clientAppId
74
78
}
79
+
75
80
# Step 2. Create a delegated permission that grants the client app access to the
76
81
# API, on behalf of the user. (This example assumes that an existing delegated
77
82
# permission grant does not already exist, in which case it would be necessary
@@ -84,6 +89,7 @@ $grant = New-MgOauth2PermissionGrant -ResourceId $resourceSp.Id `
84
89
-ClientId $clientSp.Id `
85
90
-ConsentType "Principal" `
86
91
-PrincipalId $user.Id
92
+
87
93
# Step 3. Assign the app to the user. This ensures that the user can sign in if assignment
88
94
# is required, and ensures that the app shows up under the user's My Apps.
89
95
if ($clientSp.AppRoles | ? { $_.AllowedMemberTypes -contains "User" }) {
0 commit comments