다음을 통해 공유


PowerShell을 사용하여 단일 사용자를 대신하여 동의 허용

이 문서에서는 PowerShell을 사용하여 단일 사용자를 대신하여 동의를 부여하는 방법을 알아봅니다.

사용자가 직접 동의하면 다음 이벤트가 더 자주 발생합니다.

  1. 클라이언트 애플리케이션에 대한 서비스 주체가 아직 없는 경우 만들어집니다. 서비스 주체는 Microsoft Entra 테넌트에 있는 애플리케이션 또는 서비스의 인스턴스입니다. 앱 또는 서비스에 부여된 액세스 권한은 이 서비스 주체 개체와 연결됩니다.

  2. 애플리케이션에 액세스가 필요한 각 API에 대해 애플리케이션에 필요한 권한에 대해 해당 API에 대한 위임된 권한 부여가 만들어집니다. 사용자를 대신하여 액세스 권한이 부여됩니다. 위임된 권한 부여는 사용자가 로그인할 때 애플리케이션이 사용자를 대신하여 API에 액세스할 수 있는 권한을 부여합니다.

  3. 사용자에게 클라이언트 애플리케이션이 할당됩니다. 사용자에게 애플리케이션을 할당하면 해당 사용자의 내 앱 포털에 애플리케이션이 나열됩니다. 사용자는 내 앱 포털에서 사용자를 대신하여 부여된 액세스를 검토하고 해지할 수 있습니다.

필수 조건

한 사용자를 대신하여 애플리케이션에 대한 동의를 허용하려면 다음이 필요합니다.

  • Privileged Role 관리istrator, Application 관리istrator 또는 Cloud Application 관리istrator가 있는 사용자 계정

시작하기 전에 Microsoft Entra 관리 센터에서 다음 세부 정보를 기록합니다.

  • 동의를 허용하는 앱의 앱 ID. 이 문서의 목적을 위해 클라이언트 애플리케이션이라고 합니다.
  • 클라이언트 애플리케이션에 필요한 API 권한입니다. API의 앱 ID와 권한 ID 또는 클레임 ​​값을 확인합니다.
  • 사용자 대신 액세스 권한이 부여된 사용자의 사용자 이름 또는 개체 ID입니다.

이 예제에서는 Microsoft Graph PowerShell을 사용하여 단일 사용자를 대신하여 동의를 부여합니다. 클라이언트 애플리케이션은 Microsoft Graph Explorer이며, Microsoft Graph API에 대한 액세스 권한을 부여합니다.

Microsoft Graph PowerShell을 사용하여 한 사용자를 대신하여 애플리케이션에 동의를 부여하려면 적어도 클라우드 애플리케이션 관리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를 사용하여 한 사용자를 대신하여 애플리케이션에 동의를 부여하려면 적어도 클라우드 애플리케이션 관리istrator로 Graph Explorer에 로그인합니다.

다음 권한에 동의해야 합니다.

Application.ReadWrite.All, Directory.ReadWrite.All. DelegatedPermissionGrant.ReadWrite.All

다음 예제에서는 단일 사용자를 대신하여 리소스 API에 정의된 위임된 권한을 클라이언트 엔터프라이즈 애플리케이션에 부여합니다.

이 예제에서 리소스 엔터프라이즈 애플리케이션은 개체 ID 11112222-bbbb-3333-cccc-4444dddd5555의 Microsoft Graph입니다. Microsoft Graph는 위임된 권한 User.Read.AllGroup.Read.All을 정의합니다. consentType은 테넌트의 모든 단일 사용자를 대신하여 동의함을 나타내는 Principal입니다. 클라이언트 엔터프라이즈 애플리케이션의 개체 ID는 00001111-aaaa-2222-bbbb-3333cccc4444입니다. 사용자의 principalId는 aaaaaaaa-bbbb-cccc-1111-222222222222입니다.

주의

주의가 필요합니다! 프로그래밍 방식으로 부여된 권한은 검토 또는 확인의 대상이 아닙니다. 해당 권한은 즉시 적용됩니다.

  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": "00001111-aaaa-2222-bbbb-3333cccc4444",
       "consentType": "Principal",
       "resourceId": "11112222-bbbb-3333-cccc-4444dddd5555",
       "principalId": "aaaaaaaa-bbbb-cccc-1111-222222222222",
       "scope": "User.Read.All Group.Read.All"
    }
    
  3. 다음 요청을 실행하여 사용자에게 동의를 부여했는지 확인합니다.

    GET https://graph.microsoft.com/v1.0/oauth2PermissionGrants?$filter=clientId eq '00001111-aaaa-2222-bbbb-3333cccc4444' and consentType eq 'Principal'
    
  4. 사용자에게 앱을 할당합니다. 이 할당은 할당이 필요한 경우 사용자가 로그인할 수 있도록 하고 사용자의 내 앱 포털을 통해 앱을 사용할 수 있도록 합니다. 다음 예제에서 resourceId은(는) 사용자가 할당되는 클라이언트 앱을 나타냅니다. 사용자에게 기본 앱 역할이 할당됩니다 00000000-0000-0000-0000-000000000000.

        POST /servicePrincipals/resource-servicePrincipal-id/appRoleAssignedTo
    
        {
        "principalId": "aaaaaaaa-bbbb-cccc-1111-222222222222",
        "resourceId": "a0a0a0a0-bbbb-cccc-dddd-e1e1e1e1e1e1",
        "appRoleId": "00000000-0000-0000-0000-000000000000"
        }
    

다음 단계