@@ -8,7 +8,7 @@ manager: CelesteDG
8
8
ms.service : app-service
9
9
ms.topic : tutorial
10
10
ms.workload : identity
11
- ms.date : 08/19/2022
11
+ ms.date : 04/05/2023
12
12
ms.author : ryanwi
13
13
ms.reviewer : stsoneff
14
14
ms.devlang : csharp, javascript
@@ -54,32 +54,38 @@ When accessing the Microsoft Graph, the managed identity needs to have proper pe
54
54
# [ PowerShell] ( #tab/azure-powershell )
55
55
56
56
``` powershell
57
- # Install the module. (You need admin on the machine.)
58
- # Install-Module AzureAD.
57
+ # Install the module.
58
+ # Install-Module Microsoft.Graph -Scope CurrentUser
59
59
60
- # Your tenant ID (in the Azure portal, under Azure Active Directory > Overview).
61
- $TenantID="<tenant-id>"
62
- $resourceGroup = "securewebappresourcegroup"
63
- $webAppName="SecureWebApp-20201102125811"
60
+ # The tenant ID
61
+ $TenantId = "11111111-1111-1111-1111-111111111111"
64
62
65
- # Get the ID of the managed identity for the web app.
66
- $spID = (Get-AzWebApp -ResourceGroupName $resourceGroup -Name $webAppName).identity.principalid
63
+ # The name of your web app, which has a managed identity.
64
+ $webAppName = "SecureWebApp-20201106120003"
65
+ $resourceGroupName = "SecureWebApp-20201106120003ResourceGroup"
67
66
68
- # Check the Microsoft Graph documentation for the permission you need for the operation .
69
- $PermissionName = "User.Read.All"
67
+ # The name of the app role that the managed identity should be assigned to .
68
+ $appRoleName = "User.Read.All"
70
69
71
- Connect-AzureAD -TenantId $TenantID
70
+ # Get the web app's managed identity's object ID.
71
+ Connect-AzAccount -Tenant $TenantId
72
+ $managedIdentityObjectId = (Get-AzWebApp -ResourceGroupName $resourceGroupName -Name $webAppName).identity.principalid
72
73
73
- # Get the service principal for Microsoft Graph.
74
- # First result should be AppId 00000003-0000-0000-c000-000000000000
75
- $GraphServicePrincipal = Get-AzureADServicePrincipal -SearchString "Microsoft Graph" | Select-Object -first 1
74
+ Connect-MgGraph -TenantId $TenantId -Scopes 'Application.Read.All','AppRoleAssignment.ReadWrite.All'
76
75
77
- # Assign permissions to the managed identity service principal.
78
- $AppRole = $GraphServicePrincipal.AppRoles | `
79
- Where-Object {$_.Value -eq $PermissionName -and $_.AllowedMemberTypes -contains "Application"}
76
+ # Get Microsoft Graph app's service principal and app role.
77
+ $serverApplicationName = "Microsoft Graph"
78
+ $serverServicePrincipal = (Get-MgServicePrincipal -Filter "DisplayName eq '$serverApplicationName'")
79
+ $serverServicePrincipalObjectId = $serverServicePrincipal.Id
80
80
81
- New-AzureAdServiceAppRoleAssignment -ObjectId $spID -PrincipalId $spID `
82
- -ResourceId $GraphServicePrincipal.ObjectId -Id $AppRole.Id
81
+ $appRoleId = ($serverServicePrincipal.AppRoles | Where-Object {$_.Value -eq $appRoleName }).Id
82
+
83
+ # Assign the managed identity access to the app role.
84
+ New-MgServicePrincipalAppRoleAssignment `
85
+ -ServicePrincipalId $managedIdentityObjectId `
86
+ -PrincipalId $managedIdentityObjectId `
87
+ -ResourceId $serverServicePrincipalObjectId `
88
+ -AppRoleId $appRoleId
83
89
```
84
90
85
91
# [ Azure CLI] ( #tab/azure-cli )
0 commit comments