Berikan persetujuan atas nama satu pengguna menggunakan PowerShell

Pada artikel ini, Anda akan belajar cara memberikan persetujuan atas nama satu pengguna dengan menggunakan PowerShell.

Ketika pengguna memberikan persetujuan atas dirinya sendiri, peristiwa berikut terjadi

  1. Perwakilan layanan untuk aplikasi klien dibuat, jika belum ada. Perwakilan layanan adalah instans aplikasi atau layanan di penyewa Azure Active Directory (Azure AD) Anda. Akses yang diberikan ke aplikasi atau layanan dikaitkan dengan objek perwakilan layanan.

  2. Untuk setiap API yang memerlukan akses aplikasi, pemberian izin yang didelegasikan ke API tersebut dibuat untuk izin yang diperlukan oleh aplikasi, untuk akses atas nama pengguna. Pemberian izin yang didelegasikan mengotorisasi aplikasi untuk mengakses API atas nama pengguna, saat pengguna tersebut telah masuk.

  3. Pengguna diberikan aplikasi klien. Menetapkan aplikasi kepada pengguna memastikan bahwa aplikasi tersebut terdaftar di portal My Apps untuk pengguna tersebut, yang memungkinkan mereka untuk meninjau dan mencabut akses yang telah diberikan atas nama mereka.

Prasyarat

Untuk memberikan izin ke aplikasi atas nama satu pengguna, Anda memerlukan:

  • Akun pengguna dengan Administrator Global, Administrator Aplikasi, atau Administrator Aplikasi Cloud

Sebelum memulai, catat detail berikut ini dari portal Microsoft Azure:

  • ID aplikasi untuk aplikasi yang Anda berikan persetujuan. Untuk tujuan artikel ini, kami akan menyebutnya sebagai "aplikasi klien."
  • Izin API yang diperlukan oleh aplikasi klien. Cari tahu ID aplikasi API dan ID izin atau nilai klaim.
  • Nama pengguna atau ID objek untuk pengguna atas nama yang aksesnya akan diberikan.

Untuk contoh ini, kami akan menggunakan Microsoft Graph PowerShell untuk memberikan persetujuan atas nama satu pengguna. Aplikasi klien adalah Microsoft Graph Explorer, dan kami memberikan akses ke Microsoft Graph API.

# 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 -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 does not 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.
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 access panel.")
} 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"
}

Langkah berikutnya