# Limit Permissions to a Specific Mailbox

## Limit Permissions to a Specific Mailbox

{% hint style="warning" %}
When you have created an App Registration with Mail.ReadWrite permissions it is possible to read **all** mailboxes in the directory. Follow these instructions to limit the access to mailboxes in a specific security group.
{% endhint %}

For more information about limiting the permissions to a specific mailbox, read more at <https://docs.microsoft.com/en-us/graph/auth-limit-mailbox-access>

### Create a New Mail-Enabled Security Group

{% hint style="info" %}
You can use an existing security group for your mailbox if you already have one that matches your needs. Continue with [Create an Application Access Policy](https://docs.novacura.com/extensibility-and-integration/products/active-directory/microsoft-graph/create-app-registration#create-an-application-access-policy) in that case.
{% endhint %}

* Navigate to Exchange Admin Center, <https://admin.exchange.microsoft.com>
* Create a new mail-enabled security group

<div align="left"><img src="https://3010335096-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjcNODv1kQRDGTj76t9%2F-Mkk_gD4pxmVk7mzWPC9%2F-MkklfS6BlTq4ub1jpEi%2Femail-group-1.png?alt=media&#x26;token=43d9fe17-613a-4ed6-807d-35f20deea0b0" alt=""></div>

* Select "Mail-enabled security" for the group type

<div align="left"><img src="https://3010335096-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjcNODv1kQRDGTj76t9%2F-Mkk_gD4pxmVk7mzWPC9%2F-MkklxrjXAYI46C1LWpb%2Femail-group-2.png?alt=media&#x26;token=8a2790b3-80b2-4043-917b-bd231a249c2b" alt=""></div>

* Enter a name for the group

<div align="left"><img src="https://3010335096-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjcNODv1kQRDGTj76t9%2F-Mkk_gD4pxmVk7mzWPC9%2F-MkkmAx1lN93A5F-8U20%2Femail-group-3.png?alt=media&#x26;token=1c24dd94-f48a-4c43-9738-f6d3c54ed7ca" alt=""></div>

* Enter an email address

<div align="left"><img src="https://3010335096-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjcNODv1kQRDGTj76t9%2F-Mkk_gD4pxmVk7mzWPC9%2F-MkkmVfFSHVV7YqitfqF%2Femail-group-4.png?alt=media&#x26;token=d8ed1280-153c-4a13-9d6f-2637bf4e683f" alt=""></div>

* Save the new group

<div align="left"><img src="https://3010335096-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjcNODv1kQRDGTj76t9%2F-Mkk_gD4pxmVk7mzWPC9%2F-MkkmomXKJIIA2yWmWB6%2Femail-group-5.png?alt=media&#x26;token=99e4cabf-f79c-4a8a-87cd-5c170b52fd92" alt=""></div>

* Add one or members to the new group by clicking on the Group Name and select Members in the panel to the right

<div align="left"><img src="https://3010335096-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjcNODv1kQRDGTj76t9%2F-Mkk_gD4pxmVk7mzWPC9%2F-MkknHr0ZxE4lq9OsgiH%2Femail-group-6.png?alt=media&#x26;token=ad78d1a4-098c-4f41-94be-24a61ab90d8a" alt=""></div>

* Add the member and save

<div align="left"><img src="https://3010335096-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjcNODv1kQRDGTj76t9%2F-Mkk_gD4pxmVk7mzWPC9%2F-Mkknl7ATFs2yxBtIspy%2Femail-group-7.png?alt=media&#x26;token=6c7ca5dc-7af4-4b4f-b263-62e585aec1b1" alt=""></div>

### Create an Application Access Policy

Follow these steps to connect your App Registration with the Mail-enabled Security Group by using Exchange Online PowerShell.

If you haven't used Exchange Online PowerShell before you need to set up your PowerShell environment first. Follow the instructions at [Install and maintain the EXO V2 module](https://docs.microsoft.com/en-us/powershell/exchange/exchange-online-powershell-v2?view=exchange-ps#install-and-maintain-the-exo-v2-module)

* Execute the following statements in PowerShell console
* First import the EchangeOnlineManagement module

```
Import-Module ExchangeOnlineManagement
```

* Connect to EchangeOnlineManagement

```
Connect-ExchangeOnline -UserPrincipalName admin-user@flowington.com
```

* Create the new access policy by connecting your Application ID your Security Group

```
New-ApplicationAccessPolicy -AppId B1A82AD6-34A8-4546-8BBD-A4B79625C74F -PolicyScopeGroupId flow-mailboxes@flowington.com -AccessRight RestrictAccess -Description "Restrict this app to members of distribution group Flow-Mailboxes."
```

* Test your new policy with an email address that is a member of the group and one that's not

```
Test-ApplicationAccessPolicy -Identity daisy@flowington.com -AppId B1A82AD6-34A8-4546-8BBD-A4B79625C74F
```

Sample output of an access policy test:

```
PS /Users/anbese> Test-ApplicationAccessPolicy -Identity daisy@flowington.com -AppId 0dba3db1-xxxx

RunspaceId        : -
AppId             : -
Mailbox           : Daisy
MailboxId         : -
MailboxSid        : -
AccessCheckResult : Granted


PS /Users/anbese> Test-ApplicationAccessPolicy -Identity lily@flowington.com -AppId 0dba3db1-xxxx

RunspaceId        : -
AppId             : -
Mailbox           : lily
MailboxId         : -
MailboxSid        : -
AccessCheckResult : Denied
```

* Disconnect from Exchange Online PowerShell

```
Disconnect-ExchangeOnline
```
