Tuesday, December 1, 2026

Scoping Microsoft Graph Mail Permissions to a Single Mailbox with Exchange Online RBAC for Applications

Automations that read reports from a shared mailbox or send notifications from a service mailbox usually run as a daemon application. They use the OAuth 2.0 client credentials flow with no signed-in user. The usual way to give such an app access is to grant Microsoft Graph application permissions such as Mail.Read or Mail.Send in Microsoft Entra ID.

The problem is scope. An Entra-consented Mail.Read application permission lets the app read every mailbox in the tenant. Few security teams will approve that for an automation that only needs one mailbox.

RBAC for Applications in Exchange Online fixes this. You grant the same Graph permissions through Exchange Online role assignments and pair each assignment with a resource scope, so the app can only reach the mailboxes you choose. This article covers the model, a step-by-step setup that limits Mail.Read and Mail.Send to one mailbox, how to verify it, and a complete commented PowerShell script.

How the model works

RBAC for Applications extends the existing Exchange Online RBAC model to applications. Three objects work together:

ObjectWhat it isCmdlet
Service principal pointerAn Exchange object that points to the app's existing service principal in Entra ID. Exchange can't create service principals itself; it only references themNew-ServicePrincipal
Resource scopeThe set of mailboxes the app may access. This is either a management scope (a recipient filter) or an Entra administrative unitNew-ManagementScope
Role assignmentLinks the principal to an application role (for example Application Mail.Read) and to the scopeNew-ManagementRoleAssignment
 Service principal  --->  Application role  --->  Resource scope
 (who)                    (what)                  (which mailboxes)
        \___________ New-ManagementRoleAssignment ___________/

Two authorities, one union

Exchange RBAC grants and Entra ID consents are independent and additive. The app's effective access is the union of the two. If the app keeps a tenant-wide Mail.Read grant in Entra ID, a scoped Mail.Read assignment in Exchange restricts nothing. Scoping only works if the Entra grant for the same permission is removed or was never consented.

Relationship to Application Access Policies

RBAC for Applications replaces Application Access Policies (New-ApplicationAccessPolicy). Access policies could only narrow permissions consented in Entra ID. RBAC for Applications grants scoped permissions directly, so new automations should use it.


Prerequisites

RequirementDetail
App registrationAn Entra ID app registration with a service principal (Enterprise application) in the tenant, set up for client-credentials authentication (certificate or secret)
Exchange permissionsMembership in the Organization Management role group, or a delegating assignment for the application roles
Entra permissionsThe Exchange Administrator role (or higher)
PowerShell modulesExchangeOnlineManagement and Microsoft.Graph.Applications (for the service principal lookup and the consent audit)
Target mailboxThe mailbox the automation should access, for example a shared mailbox

Step 1 — Get the correct IDs

New-ServicePrincipal needs two values:

  • AppId: the application (client) ID.
  • ObjectId: the object ID of the service principal, which is the Enterprise applications object.
⚠️ Common mistake: The object ID on the App registrations blade is the application object, not the service principal. Using it creates a pointer that never matches the token, and Graph calls fail with 403 Forbidden. Always take the ObjectId from Enterprise applications or from Get-MgServicePrincipal.
$AppId = "00000000-0000-0000-0000-000000000000"   # Application (client) ID

Connect-MgGraph -Scopes "Application.Read.All"
$sp = Get-MgServicePrincipal -Filter "appId eq '$AppId'"
$SpObjectId = $sp.Id
$SpObjectId

Step 2 — Create the service principal pointer in Exchange Online

Connect-ExchangeOnline

$exoSp = Get-ServicePrincipal -Identity $AppId -ErrorAction SilentlyContinue
if ($exoSp) {
    "Pointer already exists. ObjectId: $($exoSp.ObjectId)"
} else {
    New-ServicePrincipal -AppId $AppId -ObjectId $SpObjectId -DisplayName "Mailbox Automation"
}

If a pointer already exists, check that its ObjectId matches the service principal object ID from Step 1. If it doesn't, remove it with Remove-ServicePrincipal and create it again.

If the service principal is later deleted in Entra ID, Exchange removes the pointer and its role assignments automatically.


Step 3 — Define the resource scope

A management scope picks mailboxes with a recipient filter. To limit access to one mailbox, filter on its primary SMTP address:

$Mailbox   = "automation-mailbox@contoso.com"
$ScopeName = "Mailbox-Automation-Scope"

New-ManagementScope -Name $ScopeName -RecipientRestrictionFilter "PrimarySmtpAddress -eq '$Mailbox'"

Other filters work as well. For example, CustomAttribute1 -eq 'Automation' lets you add mailboxes by tagging them, and MemberOfGroup -eq '<group DN>' uses direct members of a group. Nested membership isn't evaluated.

Alternative: Instead of a management scope, pass an Entra administrative unit ID to -RecipientAdministrativeUnitScope on the role assignment.
Note: Exclusive management scopes do not restrict application access. Use regular (non-exclusive) scopes.

Step 4 — Assign application roles

Application roles are a special type of management role that can only be assigned to service principals. List them with:

Get-ManagementRole | Where-Object { $_.Name -like "Application *" } | Select-Object Name

Commonly used roles:

Exchange application roleGraph permissionUse case
Application Mail.ReadMail.ReadRead messages and download attachments
Application Mail.ReadBasicMail.ReadBasicRead messages without body or attachments
Application Mail.ReadWriteMail.ReadWriteRead, move, delete and update messages (no send)
Application Mail.SendMail.SendSend mail as the mailbox
Application Calendars.ReadWriteCalendars.ReadWriteManage calendar events
Application MailboxSettings.ReadMailboxSettings.ReadRead mailbox settings
Application EWS.AccessAsAppEWS.AccessAsAppEWS access (legacy workloads)

Assign read and send, both limited to the scope:

New-ManagementRoleAssignment -App $SpObjectId -Role "Application Mail.Read" -CustomResourceScope $ScopeName
New-ManagementRoleAssignment -App $SpObjectId -Role "Application Mail.Send" -CustomResourceScope $ScopeName

Follow least privilege: if the automation only reads messages, don't assign Mail.Send.


Step 5 — Verify the authorization

Test-ServicePrincipalAuthorization simulates the RBAC evaluation for a target mailbox and bypasses the permission cache, so it reflects changes right away.

Test-ServicePrincipalAuthorization -Identity $SpObjectId -Resource $Mailbox | Format-Table

Expected result for the target mailbox:

RoleName               GrantedPermissions AllowedResourceScope     ScopeType            InScope
--------               ------------------ --------------------     ---------            -------
Application Mail.Read  Mail.Read          Mailbox-Automation-Scope CustomRecipientScope True
Application Mail.Send  Mail.Send          Mailbox-Automation-Scope CustomRecipientScope True

Run a negative test against any other mailbox. Every row should show InScope = False:

Test-ServicePrincipalAuthorization -Identity $SpObjectId -Resource "someone-else@contoso.com" | Format-Table
This cmdlet evaluates Exchange RBAC only. It does not include permissions consented in Entra ID. That's why the next step matters.

Step 6 — Remove overlapping tenant-wide consent in Entra ID

Because permissions from the two systems are added together, check that the app has no admin-consented Microsoft Graph application permissions for the same capabilities:

  1. Go to Entra admin center → App registrations → your app → API permissions.
  2. Look for Microsoft Graph Application-type permissions such as Mail.Read, Mail.ReadWrite or Mail.Send.
  3. Remove them and revoke admin consent.

The complete script at the end of this article includes a read-only audit that lists any such Entra grants.


Step 7 — Call Microsoft Graph from the automation

The automation gets an app-only token in the usual way (client credentials, https://graph.microsoft.com/.default). The token won't contain Mail.Read or Mail.Send in its roles claim, and that's expected: Exchange Online authorizes the request against the RBAC assignment.

Always address the mailbox explicitly. App-only tokens can't use /me.

OperationEndpoint
List messagesGET /users/{mailbox}/messages
Download an attachmentGET /users/{mailbox}/messages/{id}/attachments/{attachmentId}
Send mailPOST /users/{mailbox}/sendMail

A request for a mailbox outside the scope returns 403 Forbidden. That's the intended behavior.

Propagation and caching

Changes to app permissions are cached for 30 minutes to 2 hours:

  • An app making no API calls has its cache reset after about 30 minutes.
  • An active app keeps its cache for up to 2 hours.

Plan for this window before end-to-end testing. Test-ServicePrincipalAuthorization is not affected by the cache.


Maintenance and rollback

TaskCommand
List the app's assignmentsGet-ManagementRoleAssignment -RoleAssignee <SpObjectId>
Change the scope of an assignmentSet-ManagementRoleAssignment -Identity <name> -CustomResourceScope <scope>
Remove an assignmentRemove-ManagementRoleAssignment -Identity <name>
Change the scope filterSet-ManagementScope -Identity <scope> -RecipientRestrictionFilter "<filter>"
Remove the pointerRemove-ServicePrincipal -Identity <AppId or ObjectId>

Limitations to be aware of

  • Applications can't be members of role groups. Assignments are always direct.
  • Application roles can only be assigned to service principals and can't be copied or derived.
  • Exclusive management scopes don't restrict app access.
  • The Autodiscover service isn't available when using RBAC application roles.
  • Supported protocols are Microsoft Graph and EWS.
  • Up to 10,000 applications per organization can use RBAC for Applications.

Complete script

Run the script line by line or section by section. Set the variables at the top first. The script is idempotent: it checks for existing objects before creating them.

# =====================================================================
#  Exchange Online RBAC for Applications
#  Restrict an app's Microsoft Graph mail permissions to one mailbox
# =====================================================================
#  Requires : ExchangeOnlineManagement, Microsoft.Graph.Applications
#  Rights   : Exchange "Organization Management" + Entra "Exchange Administrator"
# =====================================================================

# ---------------------------------------------------------------------
# Variables - update these before running
# ---------------------------------------------------------------------
$AppId        = "00000000-0000-0000-0000-000000000000"   # Application (client) ID
$Mailbox      = "automation-mailbox@contoso.com"         # Mailbox the app may access
$ScopeName    = "Mailbox-Automation-Scope"               # Management scope name
$DisplayName  = "Mailbox Automation"                     # Friendly name for the Exchange pointer
$Roles        = @("Application Mail.Read", "Application Mail.Send")   # Roles to grant (least privilege)
$TestMailbox  = "someone-else@contoso.com"               # Out-of-scope mailbox for the negative test

# ---------------------------------------------------------------------
# Connect to Microsoft Graph (read-only) and Exchange Online
# ---------------------------------------------------------------------
Connect-MgGraph -Scopes "Application.Read.All" -NoWelcome
Connect-ExchangeOnline -ShowBanner:$false

# ---------------------------------------------------------------------
# Step 1 - Resolve the service principal (Enterprise application) object ID
#          Do NOT use the object ID shown on the App registrations blade
# ---------------------------------------------------------------------
$sp = Get-MgServicePrincipal -Filter "appId eq '$AppId'"
if (-not $sp) {
    throw "No service principal found for AppId $AppId. Create the Enterprise application first."
}
$SpObjectId = $sp.Id
Write-Host "Service principal object ID: $SpObjectId" -ForegroundColor Cyan

# ---------------------------------------------------------------------
# Step 2 - Create (or validate) the service principal pointer in Exchange
# ---------------------------------------------------------------------
$exoSp = Get-ServicePrincipal -Identity $AppId -ErrorAction SilentlyContinue
if ($exoSp) {
    if ($exoSp.ObjectId -ne $SpObjectId) {
        Write-Warning "Existing pointer ObjectId ($($exoSp.ObjectId)) does not match the service principal. Recreating it."
        Remove-ServicePrincipal -Identity $AppId -Confirm:$false
        New-ServicePrincipal -AppId $AppId -ObjectId $SpObjectId -DisplayName $DisplayName
    } else {
        Write-Host "Exchange service principal pointer already exists and is valid." -ForegroundColor Green
    }
} else {
    New-ServicePrincipal -AppId $AppId -ObjectId $SpObjectId -DisplayName $DisplayName
    Write-Host "Exchange service principal pointer created." -ForegroundColor Green
}

# ---------------------------------------------------------------------
# Step 3 - Create (or reuse) the management scope for the target mailbox
# ---------------------------------------------------------------------
$Filter = "PrimarySmtpAddress -eq '$Mailbox'"
$scope  = Get-ManagementScope -Identity $ScopeName -ErrorAction SilentlyContinue
if ($scope) {
    Write-Host "Management scope '$ScopeName' already exists. Filter: $($scope.RecipientFilter)" -ForegroundColor Yellow
} else {
    New-ManagementScope -Name $ScopeName -RecipientRestrictionFilter $Filter
    Write-Host "Management scope '$ScopeName' created." -ForegroundColor Green
}

# ---------------------------------------------------------------------
# Step 4 - Assign each application role limited to the scope
#          Skips roles that are already assigned
# ---------------------------------------------------------------------
$existing = Get-ManagementRoleAssignment -RoleAssignee $SpObjectId -ErrorAction SilentlyContinue
foreach ($role in $Roles) {
    $match = $existing | Where-Object { $_.Role -eq $role }
    if ($match) {
        Write-Host "Role '$role' already assigned (scope: $($match.CustomResourceScope))." -ForegroundColor Yellow
    } else {
        New-ManagementRoleAssignment -App $SpObjectId -Role $role -CustomResourceScope $ScopeName
        Write-Host "Role '$role' assigned with scope '$ScopeName'." -ForegroundColor Green
    }
}

# ---------------------------------------------------------------------
# Step 5 - Verify: target mailbox should be InScope = True
#          This test bypasses the permission cache
# ---------------------------------------------------------------------
Write-Host "`nAuthorization for target mailbox ($Mailbox):" -ForegroundColor Cyan
Test-ServicePrincipalAuthorization -Identity $SpObjectId -Resource $Mailbox | Format-Table RoleName, GrantedPermissions, AllowedResourceScope, InScope

# Negative test: an out-of-scope mailbox should be InScope = False
Write-Host "Authorization for out-of-scope mailbox ($TestMailbox):" -ForegroundColor Cyan
Test-ServicePrincipalAuthorization -Identity $SpObjectId -Resource $TestMailbox | Format-Table RoleName, GrantedPermissions, AllowedResourceScope, InScope

# ---------------------------------------------------------------------
# Step 6 - Audit (read-only): find tenant-wide Graph mail permissions
#          consented in Entra ID. These would override the scoping.
# ---------------------------------------------------------------------
$GraphAppId   = "00000003-0000-0000-c000-000000000000"   # Microsoft Graph (well-known ID)
$graphSp      = Get-MgServicePrincipal -Filter "appId eq '$GraphAppId'"
$assignments  = Get-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $SpObjectId -All |
                Where-Object { $_.ResourceId -eq $graphSp.Id }

$mailGrants = foreach ($a in $assignments) {
    $roleValue = ($graphSp.AppRoles | Where-Object { $_.Id -eq $a.AppRoleId }).Value
    if ($roleValue -like "Mail*") { $roleValue }
}

if ($mailGrants) {
    Write-Warning "Tenant-wide Graph application permissions consented in Entra ID: $($mailGrants -join ', ')"
    Write-Warning "Remove these in Entra ID (API permissions) or the mailbox scoping has no effect."
} else {
    Write-Host "No tenant-wide Graph mail permissions found in Entra ID. Scoping is effective." -ForegroundColor Green
}

# ---------------------------------------------------------------------
# Step 7 - Summary of the app's Exchange role assignments
# ---------------------------------------------------------------------
Get-ManagementRoleAssignment -RoleAssignee $SpObjectId | Format-Table Name, Role, CustomResourceScope

Write-Host "`nDone. Allow 30 minutes to 2 hours for the permission cache to refresh before end-to-end testing." -ForegroundColor Cyan

# ---------------------------------------------------------------------
# Optional rollback - uncomment to remove everything created above
# ---------------------------------------------------------------------
# Get-ManagementRoleAssignment -RoleAssignee $SpObjectId | Remove-ManagementRoleAssignment -Confirm:$false
# Remove-ManagementScope -Identity $ScopeName -Confirm:$false
# Remove-ServicePrincipal -Identity $AppId -Confirm:$false

# Disconnect sessions
Disconnect-ExchangeOnline -Confirm:$false
Disconnect-MgGraph | Out-Null

Summary

  • Entra-consented Graph mail permissions apply to the whole tenant. RBAC for Applications limits the same permissions to the mailboxes you choose.
  • The setup is three objects: a service principal pointer, a resource scope, and a role assignment.
  • Use the Enterprise application object ID, not the App registration object ID.
  • Exchange and Entra permissions are added together. Remove overlapping tenant-wide consent, or the scope has no effect.
  • Verify with Test-ServicePrincipalAuthorization (positive and negative tests), then allow up to 2 hours for caching before end-to-end tests.

Reference: Role Based Access Control for Applications in Exchange Online — Microsoft Learn

No comments:

Post a Comment

Featured Post

Scoping Microsoft Graph Mail Permissions to a Single Mailbox with Exchange Online RBAC for Applications

Automations that read reports from a shared mailbox or send notifications from a service mailbox usually run as a daemon application. They ...

Popular posts