アプリケーションへのユーザーとグループの割り当てを管理する

この記事では、PowerShell を使用して Microsoft Entra ID でエンタープライズ アプリケーションにユーザーとグループを割り当てる方法を示します。 アプリケーションにユーザーを割り当てると、そのアプリケーションが、簡単にアクセスできるようにユーザーの [マイ アプリ] ポータルに表示されます。 アプリケーションでアプリ ロールが公開されている場合は、ユーザーに特定のアプリ ロールを割り当てることもできます。

グループをアプリケーションに割り当てると、そのグループ内のユーザーのみがアクセスできます。 割り当ては、入れ子になったグループにはカスケードされません。

グループベースの割り当てには、Microsoft Entra ID P1 または P2 エディションが必要です。 グループベースの割り当ては、SecurityEnabled 設定が True に設定されているセキュリティ グループと Microsoft 365 グループでのみサポートされています。 入れ子になったグループ メンバーシップは現在サポートされていません。 この記事で説明されている機能のライセンス要件の詳細については、Microsoft Entra の価格のページを参照してください。

制御を強化するために、ユーザー割り当てを必要とするように、特定の種類のエンタープライズ アプリケーションを構成できます。 アプリにユーザー割り当てを要求する方法の詳細については、「アプリケーションへのアクセスの管理」を参照してください。

前提条件

エンタープライズ アプリケーションにユーザーを割り当てるには、次のものが必要です。

  • アクティブなサブスクリプションを持つ Microsoft Entra アカウント。 まだアカウントがない場合は、無料でアカウントを作成することができます。
  • 次のいずれかのロール: グローバル管理者、クラウド アプリケーション管理者、アプリケーション管理者、またはサービス プリンシパルの所有者。
  • グループベースの割り当て用の Microsoft Entra ID P1 または P2。 この記事で説明されている機能のライセンス要件の詳細については、Microsoft Entra の価格のページを参照してください。

ヒント

この記事の手順は、開始するポータルに応じて若干異なる場合があります。

Microsoft Entra 管理センターを使用してアプリケーションにユーザーとグループを割り当てる

エンタープライズ アプリケーションにユーザーまたはグループ アカウントを割り当てるには:

  1. クラウド アプリケーション管理者以上として Microsoft Entra 管理センターにサインインします。

  2. [ID]>[アプリケーション]>[エンタープライズ アプリケーション]>[すべてのアプリケーション] に移動します。

  3. 検索ボックスに既存のアプリケーションの名前を入力し、検索結果からアプリケーションを選択します。

  4. [ユーザーとグループ][ユーザー/グループの追加] の順に選択します。

    Assign user account to an application in your Microsoft Entra tenant.

  5. [割り当ての追加] ペインで [ユーザーとグループ][選択されていません] を選択します。

  6. アプリケーションに割り当てるユーザーまたはグループを見つけて選択します。 たとえば、contosouser1@contoso.com または contosoteam1@contoso.com です。

  7. [選択] を選択します。

  8. [ロールの選択] の下で、ユーザーまたはグループに割り当てるロールを選択します。 ロールをまだ定義していない場合、既定のロールは既定のアクセスです。

  9. [割り当ての追加] ウィンドウで、[割り当て] を選択して、アプリケーションにユーザーまたはグループを割り当てます。

アプリケーションからユーザーとグループの割り当てを解除する

  1. アプリケーションにユーザーとグループを割り当てる」セクションの手順に従って、[ユーザーとグループ] ペインに移動します。
  2. アプリケーションから割り当てを解除したいユーザーまたはグループを探して選択します。
  3. [削除] を選択して、アプリケーションからユーザーまたはグループの割り当てを解除します。

Azure AD PowerShell を使用してアプリケーションにユーザーとグループを割り当てる

  1. 管理者特権での Windows PowerShell コマンド プロンプトを開きます。

  2. Connect-AzureAD を実行して少なくともクラウド アプリケーション管理者としてサインインします。

  3. 次のスクリプトを使用して、アプリケーションにユーザーとロールを割り当てます。

    # Assign the values to the variables
    $username = "<Your user's UPN>"
    $app_name = "<Your App's display name>"
    $app_role_name = "<App role display name>"
    
    # Get the user to assign, and the service principal for the app to assign to
    $user = Get-AzureADUser -ObjectId "$username"
    $sp = Get-AzureADServicePrincipal -Filter "displayName eq '$app_name'"
    $appRole = $sp.AppRoles | Where-Object { $_.DisplayName -eq $app_role_name }
    
    # Assign the user to the app role
    New-AzureADUserAppRoleAssignment -ObjectId $user.ObjectId -PrincipalId $user.ObjectId -ResourceId $sp.ObjectId -Id $appRole.Id
    

グループをエンタープライズ アプリに割り当てるには、Get-AzureADUserGet-AzureADGroup に置き換え、New-AzureADUserAppRoleAssignmentNew-AzureADGroupAppRoleAssignment に置き換える必要があります。

アプリケーション ロールにグループを割り当てる方法の詳細については、「AzureADGroupAppRoleAssignment」のドキュメントをご覧ください。

この例では PowerShell を使用して、ユーザー Britta Simon を Microsoft Workplace Analytics アプリケーションに割り当てます。

  1. PowerShell で、変数 $username、$app_name および $app_role_name に対応する値を割り当てます。

    # Assign the values to the variables
    $username = "britta.simon@contoso.com"
    $app_name = "Workplace Analytics"
    
  2. この例では、Britta Simon に割り当てるアプリケーション ロールの正確な名前はわかりません。 次のコマンドを実行し、ユーザーの UPN とサービス プリンシパル表示名を使用してユーザー ($user) と サービス プリンシパル ($sp) を取得します。

    # Get the user to assign, and the service principal for the app to assign to
    $user = Get-AzureADUser -ObjectId "$username"
    $sp = Get-AzureADServicePrincipal -Filter "displayName eq '$app_name'"
    
  3. $sp.AppRoles コマンドを実行して、Workplace Analytics アプリケーションで使用できるロールを表示します。 この例では、Britta Simon にアナリスト (制限付きアクセス) のロールを割り当てます。 Shows the roles available to a user using Workplace Analytics Role

  4. $app_role_name 変数にロール名を割り当てます。

    # Assign the values to the variables
    $app_role_name = "Analyst (Limited access)"
    $appRole = $sp.AppRoles | Where-Object { $_.DisplayName -eq $app_role_name }
    
  5. 次のコマンドを実行して、アプリのロールにユーザーを割り当てます。

    # Assign the user to the app role
    New-AzureADUserAppRoleAssignment -ObjectId $user.ObjectId -PrincipalId $user.ObjectId -ResourceId $sp.ObjectId -Id $appRole.Id
    

Azure AD PowerShell を使用してアプリケーションからユーザーとグループの割り当てを解除する

  1. 管理者特権での Windows PowerShell コマンド プロンプトを開きます。

  2. Connect-AzureAD を実行して少なくともクラウド アプリケーション管理者としてサインインします。

  3. 次のスクリプトを使用して、アプリケーションからユーザーとロールを削除します。

    # Store the proper parameters
    $user = get-azureaduser -ObjectId <objectId>
    $spo = Get-AzureADServicePrincipal -ObjectId <objectId>
    
    #Get the ID of role assignment 
    $assignments = Get-AzureADServiceAppRoleAssignment -ObjectId $spo.ObjectId | Where {$_.PrincipalDisplayName -eq $user.DisplayName}
    
    #if you run the following, it will show you what is assigned what
    $assignments | Select *
    
    #To remove the App role assignment run the following command.
    Remove-AzureADServiceAppRoleAssignment -ObjectId $spo.ObjectId -AppRoleAssignmentId $assignments[assignment number].ObjectId
    

Azure AD PowerShell を使用してアプリケーションに割り当てられているすべてのユーザーを削除する

次のスクリプトを使って、アプリケーションに割り当てられているすべてのユーザーとグループを削除します。

#Retrieve the service principal object ID.
$app_name = "<Your App's display name>"
$sp = Get-AzureADServicePrincipal -Filter "displayName eq '$app_name'"
$sp.ObjectId

# Get Service Principal using objectId
$sp = Get-AzureADServicePrincipal -ObjectId "<ServicePrincipal objectID>"

# Get Azure AD App role assignments using objectId of the Service Principal
$assignments = Get-AzureADServiceAppRoleAssignment -ObjectId $sp.ObjectId -All $true

# Remove all users and groups assigned to the application
$assignments | ForEach-Object {
    if ($_.PrincipalType -eq "User") {
        Remove-AzureADUserAppRoleAssignment -ObjectId $_.PrincipalId -AppRoleAssignmentId $_.ObjectId
    } elseif ($_.PrincipalType -eq "Group") {
        Remove-AzureADGroupAppRoleAssignment -ObjectId $_.PrincipalId -AppRoleAssignmentId $_.ObjectId
    }
}

Microsoft Graph PowerShell を使用してアプリケーションにユーザーとグループを割り当てる

  1. 管理者特権での Windows PowerShell コマンド プロンプトを開きます。
  2. Connect-MgGraph -Scopes "Application.ReadWrite.All", "Directory.ReadWrite.All", "AppRoleAssignment.ReadWrite.All" を実行して少なくともクラウド アプリケーション管理者としてサインインします。
  3. 次のスクリプトを使用して、アプリケーションにユーザーとロールを割り当てます。

# Assign the values to the variables

$userId = "<Your user's ID>"
$app_name = "<Your App's display name>"
$app_role_name = "<App role display name>"
$sp = Get-MgServicePrincipal -Filter "displayName eq '$app_name'"

# Get the user to assign, and the service principal for the app to assign to

$params = @{
    "PrincipalId" =$userId
    "ResourceId" =$sp.Id
    "AppRoleId" =($sp.AppRoles | Where-Object { $_.DisplayName -eq $app_role_name }).Id
    }

# Assign the user to the app role

New-MgUserAppRoleAssignment -UserId $userId -BodyParameter $params |
    Format-List Id, AppRoleId, CreationTime, PrincipalDisplayName,
    PrincipalId, PrincipalType, ResourceDisplayName, ResourceId

Microsoft Graph PowerShell を使用してアプリケーションからユーザーとグループの割り当てを解除する

  1. 管理者特権での Windows PowerShell コマンド プロンプトを開きます。
  2. Connect-MgGraph -Scopes "Application.ReadWrite.All", "Directory.ReadWrite.All", "AppRoleAssignment.ReadWrite.All" を実行して少なくともクラウド アプリケーション管理者としてサインインします。 次のスクリプトを使用して、アプリケーションからユーザーとロールを削除します。

# Get the user and the service principal

$user = Get-MgUser -UserId <userid>
$spo = Get-MgServicePrincipal -ServicePrincipalId <ServicePrincipalId>

# Get the Id of the role assignment

$assignments = Get-MgServicePrincipalAppRoleAssignedTo -ServicePrincipalId $spo.Id | Where {$_.PrincipalDisplayName -eq $user.DisplayName}

# if you run the following, it will show you the list of users assigned to the application

$assignments | Select *

# To remove the App role assignment run the following command.

Remove-MgServicePrincipalAppRoleAssignedTo -AppRoleAssignmentId  '<AppRoleAssignment-id>' -ServicePrincipalId $spo.Id

Microsoft Graph PowerShell を使用してアプリケーションに割り当てられているすべてのユーザーとグループを削除する

次のスクリプトを使って、アプリケーションに割り当てられているすべてのユーザーとグループを削除します。

$assignments | ForEach-Object {
    if ($_.PrincipalType -in ("user", "Group")) {
        Remove-MgServicePrincipalAppRoleAssignedTo -ServicePrincipalId $Sp.Id -AppRoleAssignmentId $_.Id  }
}

Microsoft Graph API を使用してアプリケーションにユーザーとグループを割り当てる

  1. ユーザーとグループをアプリケーションに割り当てるには、少なくともクラウド アプリケーション管理者として Graph エクスプローラーにサインインします。

    次のアクセス許可に同意する必要があります。

    Application.ReadWrite.AllDirectory.ReadWrite.AllAppRoleAssignment.ReadWrite.All

    アプリのロールの割り当てを許可するには、以下の 3 つの識別子が必要です。

    • principalId: アプリのロールを割り当てるユーザーまたはグループの ID。
    • resourceId: アプリのロールを定義するリソース servicePrincipal の ID。
    • appRoleId: ユーザーまたはグループに割り当てる appRole (リソース サービス プリンシパルで定義される) の ID。
  2. エンタープライズ アプリケーションを取得します。 DisplayName でフィルター処理します。

    GET https://graph.microsoft.com/v1.0/servicePrincipals?$filter=displayName eq '{appDisplayName}'
    

    応答本文から以下の値を記録します。

    • エンタープライズ アプリケーションのオブジェクト ID
    • ユーザーに割り当てる appRoleId。 アプリケーションでロールが公開されていない場合、ユーザーには既定のアクセス ロールが割り当てられます。
  3. ユーザーのプリンシパル名でフィルター処理して、ユーザーを取得します。 ユーザーのオブジェクト ID を記録します。

    GET https://graph.microsoft.com/v1.0/users/{userPrincipalName}
    
  4. アプリケーションにユーザーを割り当ててください。

    POST https://graph.microsoft.com/v1.0/servicePrincipals/{resource-servicePrincipal-id}/appRoleAssignedTo
    
    {
    "principalId": "33ad69f9-da99-4bed-acd0-3f24235cb296",
    "resourceId": "9028d19c-26a9-4809-8e3f-20ff73e2d75e",
    "appRoleId": "ef7437e6-4f94-4a0a-a110-a439eb2aa8f7"
    }
    

    この例では、resource-servicePrincipal-id と resourceId の両方がエンタープライズ アプリケーションを表しています。

Microsoft Graph API を使用してアプリケーションからユーザーとグループの割り当てを解除する

アプリケーションからユーザーとグループの割り当てを解除するには、次のクエリを実行します。

  1. エンタープライズ アプリケーションを取得します。 displayName でフィルター処理します。

    GET https://graph.microsoft.com/v1.0/servicePrincipals?$filter=displayName eq '{appDisplayName}'
    
  2. アプリケーションの appRoleAssignments の一覧を取得します。

    GET https://graph.microsoft.com/v1.0/servicePrincipals/{id}/appRoleAssignedTo
    
  3. appRoleAssignments ID を指定して、appRoleAssignments を削除します。

    DELETE https://graph.microsoft.com/v1.0/servicePrincipals/{resource-servicePrincipal-id}/appRoleAssignedTo/{appRoleAssignment-id}
    

次のステップ