> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mage.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Sign in with Microsoft Entra ID aka Active Directory

> Enable signing in with a Microsoft account in Mage.

### Pre-requisites

First, you'll need to enable user authentication within Mage. You can find out more about
this in the [Overview page](https://docs.mage.ai/production/authentication/overview.md).

### Enable Microsoft sign in

There are two ways to enable Microsoft sign in. You can either register an application in your organization's
own Microsoft Entra ID, or use the default Mage application. We recommend registering your own application unless
there is a specific reason you cannot do so.

> [accordion] Register and use your own Mage application
>
> You will need to register an application in your organization's Microsoft Entra ID.
>
> **1. Log into your Azure Portal**
>
> **2. Click into the Microsoft Entra ID service**
>
> ![Azure Active Directory](https://mage-ai.github.io/assets/production/authentication/ad.png)
>
> **3. Navigate to the tenant you want users to sign into**
>
> **4. Click App registrations on the left sidebar**
>
> ![App registrations](https://mage-ai.github.io/assets/production/authentication/ad-app-registrations.png)
>
> **5. Register a new application for Mage**
>
> Click the "New registration" button at the top of the page, and fill in the details for your application.
>
> ![New registration](https://mage-ai.github.io/assets/production/authentication/ad-new-registration.png)
>
> Make sure you add the following web redirect URI: `http(s)://<your-mage-url>/oauth` (for Mage open-source)
> or  `https://cluster.mage.ai/<your-cluster-base-path>/oauth?provider=active_directory` (for Mage Pro)
> or `https://<mage-host-url>/oauth?provider=active_directory` (for Mage self-hosted).
>
> **6. Create client secret for the application**
>
> Click into the application you created, and click "Certificates & secrets" in the left sidebar.
>
> ![Certificates & secrets](https://mage-ai.github.io/assets/production/authentication/ad-certificates.png)
>
> Copy the "Application (client) ID" and the "Directory (tenant) ID" values.
>
> ![Copy values](https://mage-ai.github.io/assets/production/authentication/ad-application-fields.png)
>
> Create a new client secret and copy the value.
>
> **7. Set environment variable for Mage**
>
> Once you have registered your application, you will need to set the following environment variables
> in your Mage setup.
>
> | Name | Value |
> | - | - |
> | `ACTIVE_DIRECTORY_DIRECTORY_ID` | `<tenant_id>` |
> | `ACTIVE_DIRECTORY_CLIENT_ID` | `<application_client_id>` |
> | `ACTIVE_DIRECTORY_CLIENT_SECRET` | `<application_client_secret>` |
>
> You should now see the "Sign in with Microsoft" button when you start Mage and attempt to sign in.
> You may need to restart your Mage server for the changes to take effect.
>
> ![Sign in with Microsoft](https://mage-ai.github.io/assets/production/authentication/sign-in-with-microsoft.png)

> [accordion] Use the default Mage application
>
> You will need to get your organization's Tenant ID. You can find this in the
> Azure Portal:
>
> **1. Log into your Azure Portal**
>
> **2. Click into the Microsoft Entra ID service**
>
> ![Azure Active Directory](https://mage-ai.github.io/assets/production/authentication/ad.png)
>
> **3. Navigate to the directory you want users to sign into**
>
> **4. Click 'Properties' in the left sidebar**
>
> ![Active Directory Properties](https://mage-ai.github.io/assets/production/authentication/ad-properties.png)
>
> **5. Copy the value under Tenant ID**
>
> ![Active Directory Tenant ID](https://mage-ai.github.io/assets/production/authentication/ad-tenant-id.png)
>
> **6. Set environment variable for Mage**
>
> Once you have found your tenant ID, you will need to set the following environment variable
> in your Mage setup.
>
> | | |
> | - | - |
> | Name | `ACTIVE_DIRECTORY_DIRECTORY_ID` |
> | Value | `<tenant_id>` |
>
> You should now see the "Sign in with Microsoft" button when you start Mage and attempt to sign in.
> You may need to restart your Mage server for the changes to take effect.
>
> ![Sign in with Microsoft](https://mage-ai.github.io/assets/production/authentication/sign-in-with-microsoft.png)

### Configurable environment variables

To configure the environment for Microsoft Azure Cloud and Microsoft Azure Gov Cloud sign-in,
you need to set the following environment variables:

1. You can set the `AZURE_HOST` environment variable to one of the following based on your environment:

```
Public cloud host url: "https://login.microsoftonline.com"
China host url: "https://login.chinacloudapi.cn"
Germany host url: "https://login.microsoftonline.de"
Government host url: "https://login.microsoftonline.us"
```

2. You can set the `MICROSOFT_GRAPH_API_HOST` environment variable to one of the following basd on your environment:

```
Microsoft Graph global service: "https://graph.microsoft.com"
Microsoft Graph for US Government L4: "https://graph.microsoft.us"
Microsoft Graph for US Government L5 (DOD): "https://dod-graph.microsoft.us"
Microsoft Graph China operated by 21Vianet: "https://microsoftgraph.chinacloudapi.cn"
```

### Map AD role to Mage role

> [!NOTE]
>
> You will need to have created your own application and have app roles in order for this mapping to work.

You can map Active Directory roles to Mage roles by setting the `ACTIVE_DIRECTORY_ROLES_MAPPING` environment variable.
The value should be a JSON object with the AD role as the key and the name of the Mage role as the value. For example:

`export ACTIVE_DIRECTORY_ROLES_MAPPING='{"Mage.Edit": "Editor", "Mage.Admin": "Admin"}'`

You can find the AD role value in the App roles section of your application in the Azure Portal.

![App roles](https://mage-ai.github.io/assets/production/authentication/ad-app-role-value.png)

When a user signs in for the first time with their Microsoft account, Mage will use this mapping to automatically add
the appropriate roles to the user.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.