使用 PowerShell 代表單一使用者授與同意

在本文中,您將瞭解如何使用PowerShell代表單一使用者授與同意。

當使用者自行授與同意時,通常會發生下列事件:

  1. 如果用戶端應用程式尚未存在,則會建立用戶端應用程式的服務主體。 服務主體是 Microsoft Entra 租使用者中應用程式或服務的實例。 授與應用程式或服務的存取權與此服務主體對象相關聯。

  2. 針對應用程式需要存取的每個 API,會針對應用程式所需的許可權建立委派許可權授與給該 API。 存取權會代表使用者授與。 委派的許可權授與會授權應用程式在使用者登入時代表使用者存取 API。

  3. 用戶已獲指派用戶端應用程式。 將應用程式指派給使用者,可確保該應用程式列在該使用者的 我的應用程式 入口網站中。 用戶可以檢閱和撤銷代表其 我的應用程式 入口網站授與的存取權。

必要條件

若要代表一位使用者授與應用程式同意,您需要:

  • 具有 Global 管理員 istrator、Application 管理員 istrator 或 Cloud Application 管理員 istrator 的用戶帳戶

開始之前,請從 Microsoft Entra 系統管理中心記錄下列詳細資料:

  • 您授與同意之應用程式的應用程式識別碼。 針對本文的目的,我們會將其稱為用戶端應用程式。
  • 用戶端應用程式所需的 API 許可權。 瞭解 API 的應用程式識別碼和許可權標識碼或宣告值。
  • 代表存取權之使用者的用戶名稱或物件標識碼。

在此範例中,我們使用 Microsoft Graph PowerShell 代表單一使用者授與同意。 用戶端應用程式是 Microsoft Graph 總管,我們會授與 Microsoft Graph API 的存取權。

若要使用 Microsoft Graph PowerShell 代表一位使用者授與應用程式同意,您必須至少以 Cloud Application 管理員 istrator 身分登入。

# 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 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 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"
}

若要使用 Microsoft Graph API 代表一位使用者授與應用程式同意,請至少以 Cloud Application 管理員 istrator 身分登入 Graph 總管。

您必須同意下列權限:

Application.ReadWrite.All、、Directory.ReadWrite.AllDelegatedPermissionGrant.ReadWrite.All

在下列範例中,您會代表單一使用者,將資源 API 所定義的委派許可權授與客戶端應用程式。

在此範例中,資源企業應用程式是對象標識碼 7ea9e944-71ce-443d-811c-71e8047b557a的 Microsoft Graph。 Microsoft Graph 會定義委派的許可權, User.Read.All 以及 Group.Read.All。 consentType 是 Principal,表示您代表租使用者中的單一使用者同意。 用戶端應用程式的物件識別碼為 b0d9b9e3-0ecf-4bfd-8dab-9273dd055a941。 使用者的 principalId 為 3fbd929d-8c56-4462-851e-0eb9a7b3a2a5

警告

小心! 以程序設計方式授與的許可權不受檢閱或確認。 他們立即生效。

  1. 擷取您租使用者應用程式中 Microsoft graph (資源應用程式) 所定義的所有委派許可權。 識別您想要授與用戶端應用程式的委派許可權。 在此範例中,委派許可權為 User.Read.AllGroup.Read.All

    GET https://graph.microsoft.com/v1.0/servicePrincipals?$filter=displayName eq 'Microsoft Graph'&$select=id,displayName,appId,oauth2PermissionScopes
    
  2. 執行下列要求,以代表使用者將委派的許可權授與客戶端應用程式。

    POST https://graph.microsoft.com/v1.0/oauth2PermissionGrants
    
    Request body
    {
       "clientId": "b0d9b9e3-0ecf-4bfd-8dab-9273dd055a94",
       "consentType": "Principal",
       "resourceId": "7ea9e944-71ce-443d-811c-71e8047b557a",
       "principalId": "3fbd929d-8c56-4462-851e-0eb9a7b3a2a5",
       "scope": "User.Read.All Group.Read.All"
    }
    
  3. 執行下列要求,確認您已對使用者授與同意。

    GET https://graph.microsoft.com/v1.0/oauth2PermissionGrants?$filter=clientId eq 'b0d9b9e3-0ecf-4bfd-8dab-9273dd055a94' and consentType eq 'Principal'
    
  4. 將應用程式指派給使用者。 此指派可確保使用者可以在需要指派時登入,並確保應用程式可透過使用者的 我的應用程式 入口網站取得。 在下列範例中, resourceId表示要指派使用者的用戶端應用程式。 使用者獲指定的預設應用程式角色為 00000000-0000-0000-0000-000000000000

        POST /servicePrincipals/resource-servicePrincipal-id/appRoleAssignedTo
    
        {
        "principalId": "3fbd929d-8c56-4462-851e-0eb9a7b3a2a5",
        "resourceId": "b0d9b9e3-0ecf-4bfd-8dab-9273dd055a94",
        "appRoleId": "00000000-0000-0000-0000-000000000000"
        }
    

下一步