> For the complete documentation index, see [llms.txt](https://docs.maiagent.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.maiagent.ai/maiagent-user-guide/en/org/sso/sso-saml/sso-saml-adfs.md).

# ADFS Integration

Integrate your enterprise ADFS (Active Directory Federation Services) via the SAML 2.0 protocol

ADFS (Active Directory Federation Services) is Microsoft's enterprise identity federation service. Enterprises that have deployed Windows Active Directory and ADFS can let employees sign in to MaiAgent with their AD credentials, without creating separate accounts.

**Target audience**: Enterprises that have deployed ADFS (Windows Server 2016 / 2019 / 2022)

**Before you begin, prepare the following**:

* MaiAgent organization administrator permissions (used to configure the authentication source)
* Administrator permissions on the ADFS server (used to create the relying party trust and claim rules)
* An AD account for testing, whose **`mail` attribute must have a value**

{% hint style="info" %}
The basic concept of the integration is a "two-way exchange of information": enter ADFS information (Metadata URL) into MaiAgent, then import MaiAgent information (SP Metadata URL) into ADFS. For general SAML setup instructions, see [SAML Integration](https://docs.maiagent.ai/maiagent-user-guide/en/org/sso/sso-saml.md) first.
{% endhint %}

## Step 1: Create an Authentication Source in the MaiAgent Backend <a href="#create-auth-source" id="create-auth-source"></a>

Go to <mark style="color:blue;">Organization Settings</mark> > <mark style="color:blue;">Third-Party Login Settings</mark> and select the <mark style="color:blue;">SAML</mark> authentication type:

| Field                      | Value                                                                                                 |
| -------------------------- | ----------------------------------------------------------------------------------------------------- |
| Authentication Source Name | For example, `my-company` (lowercase letters, digits, and hyphens; used to generate SP endpoint URLs) |
| IdP Metadata URL           | `https://{ADFS domain}/FederationMetadata/2007-06/FederationMetadata.xml`                             |
| Email Domain               | (Optional) Enterprise Email domain, for example `company.com`                                         |

The ADFS Federation Metadata URL is a fixed path; just replace `{ADFS domain}` with your ADFS host domain. When you save, MaiAgent fetches and parses this metadata immediately and automatically fills in the Entity ID, SSO URL, and signing certificate.

{% hint style="warning" %}
The MaiAgent server must be able to reach this Metadata URL when you save. If ADFS is only accessible from the internal network and MaiAgent cannot connect, use manual mode instead: enter the IdP Entity ID (usually `http://{ADFS domain}/adfs/services/trust`), the IdP SSO URL (usually `https://{ADFS domain}/adfs/ls/`), and the **Token-Signing certificate** (Base64 format) exported from the ADFS management console.
{% endhint %}

## Step 2: Obtain the MaiAgent SP Information <a href="#get-sp-info" id="get-sp-info"></a>

After saving, the settings page displays the SP (Service Provider, i.e., the MaiAgent side) information:

| SP Field                    | Format                                                     |
| --------------------------- | ---------------------------------------------------------- |
| Entity ID / SP Metadata URL | `https://{platform domain}/accounts/saml/{name}/metadata/` |
| ACS URL                     | `https://{platform domain}/accounts/saml/{name}/acs/`      |

Open the SP Metadata URL in a browser and confirm that it displays an XML document (starting with `<EntityDescriptor>`), which means the authentication source is active. This URL is what you hand to the ADFS administrator in the next step.

{% hint style="warning" %}
For on-premises deployments that use a non-standard port (for example `https://platform-domain:8000`), check whether the `Location` of `AssertionConsumerService` in the XML **includes the port**. If the entityID has a port but the ACS URL does not, the reverse proxy (Nginx) is stripping the port from the Host header. Change `proxy_set_header Host $host;` to `proxy_set_header Host $http_host;` in your Nginx configuration and reload Nginx (note that this must be changed in every location block), then refresh and verify again.
{% endhint %}

## Step 3: Create a Relying Party Trust in ADFS <a href="#create-relying-party-trust" id="create-relying-party-trust"></a>

The following steps are performed by the ADFS administrator on the ADFS server:

1. Open <mark style="color:blue;">Server Manager</mark> > <mark style="color:blue;">Tools</mark> > <mark style="color:blue;">AD FS Management</mark>
2. On the left, select <mark style="color:blue;">Relying Party Trusts</mark>, then on the right click <mark style="color:blue;">Add Relying Party Trust...</mark>
3. Select <mark style="color:blue;">Claims aware</mark> > Start
4. On the **Select Data Source** page, choose an import method (see below)
5. Enter `MaiAgent` for **Display name**
6. For **Access Control Policy**, select <mark style="color:blue;">Permit everyone</mark> for now (change it to specific AD groups after testing passes)
7. Click Next through the rest of the wizard

**Two import methods on Select Data Source**:

| Method                                               | When to use                                                                             | Action                                                                                         |
| ---------------------------------------------------- | --------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- |
| Import data about the relying party published online | The ADFS server trusts MaiAgent's TLS certificate                                       | Paste the SP Metadata URL                                                                      |
| Import data from a file                              | MaiAgent uses a self-signed or internal certificate (common in on-premises deployments) | Open the SP Metadata URL in a browser, save the XML as a file, then select that file to import |

{% hint style="info" %}
If the online import shows the error "Could not create SSL/TLS secure channel", the ADFS server does not trust MaiAgent's certificate; use file import instead. Metadata import is a one-time action. During the actual sign-in flow, ADFS does not need to connect to MaiAgent (all redirects are performed by the user's browser), so file import has no downsides.
{% endhint %}

After a successful import, the wizard automatically fills in the Identifier (the SP Metadata URL) and the ACS Endpoint; you do not need to enter them manually.

## Step 4: Configure Claim Rules <a href="#configure-claim-rules" id="configure-claim-rules"></a>

{% hint style="danger" %}
This is the **step most easily missed** in the integration. After the wizard finishes, the claim rules are empty. If you do not configure them, the SAML response returned by ADFS carries no user attributes and sign-in fails immediately.
{% endhint %}

Right-click the Relying Party you just created > <mark style="color:blue;">Edit Claim Issuance Policy...</mark> (note: not Edit Access Control Policy) > <mark style="color:blue;">Add Rule</mark>, choose the template <mark style="color:blue;">Send Claims Using a Custom Rule</mark>, and create the following two rules in order.

**Rule 1: Send user attributes** (name: `MaiAgent attributes`)

```
c:[Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/windowsaccountname", Issuer == "AD AUTHORITY"]
 => issue(store = "Active Directory", types = ("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress", "http://schemas.microsoft.com/identity/claims/displayname"), query = ";mail,mail,displayName;{0}", param = c.Value);
```

This rule reads the user's `mail` and `displayName` attributes from AD. Note in particular that MaiAgent reads the email address from the **Name claim** (`.../claims/name`), not the commonly used emailaddress claim, so this rule sends the Email in both claims (see the [attribute mapping description in SAML Integration](https://docs.maiagent.ai/maiagent-user-guide/en/org/sso/sso-saml.md#saml-attribute-mapping) for details).

**Rule 2: Convert Email to NameID** (name: `Email to NameID`)

```
c:[Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"]
 => issue(Type = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", Issuer = c.Issuer, OriginalIssuer = c.OriginalIssuer, Value = c.Value, ValueType = c.ValueType, Properties["http://schemas.xmlsoap.org/ws/2005/05/identity/claimproperties/format"] = "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress");
```

This rule converts the Email into an Email-format NameID, which is used for sign-in identification and Single Logout.

Three things to keep in mind when configuring:

* Rule order: Rule 1 on top, Rule 2 below (Rule 2 depends on the emailaddress claim issued by Rule 1)
* Copy and paste the rule text as a whole; do not type it by hand. A single wrong character in a claim URI invalidates the entire rule
* No service restart is needed after saving; you can test sign-in right away

## Step 5: Check the Relying Party Properties <a href="#check-relying-party-properties" id="check-relying-party-properties"></a>

Right-click the Relying Party > <mark style="color:blue;">Properties</mark>:

| Tab        | What to check                                                                                                                                                               |
| ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Advanced   | Select **SHA-256** for Secure hash algorithm                                                                                                                                |
| Encryption | **Do not** place any encryption certificate. MaiAgent does not support encrypted SAML assertions; if a certificate is present, ADFS encrypts the response and sign-in fails |
| Signature  | Leave blank                                                                                                                                                                 |

## Step 6: Test Sign-In <a href="#test-login" id="test-login"></a>

1. Use a test account whose AD `mail` attribute has a value
2. On the MaiAgent sign-in page, click SSO sign-in; the browser should redirect to `https://{ADFS domain}/adfs/ls/`
3. Enter the AD username and password; after successful authentication, you are redirected back to MaiAgent

{% hint style="info" %}
Users who sign in via SSO for the first time automatically get an account and are added to the organization, but **have no permissions by default**, so they see the "<mark style="color:blue;">No Access Permission</mark>" page. This is a normal security design. An organization administrator should go to <mark style="color:blue;">Organization Settings</mark> → <mark style="color:blue;">Organization Overview</mark> → <mark style="color:blue;">Manage Actions</mark> → <mark style="color:blue;">Edit Member</mark>, find the member, and assign role permissions. After the user clicks <mark style="color:blue;">Recheck Permissions</mark> or signs in again, they can use the platform. For a full explanation of this page, see [How to Sign In to the Platform: The "No Access Permission" page after sign-in](https://docs.maiagent.ai/maiagent-user-guide/en/start/login.md#faq-no-access).
{% endhint %}

## Troubleshooting <a href="#troubleshooting" id="troubleshooting"></a>

| Symptom                                                                                                | Cause                                                                                           | Solution                                                                                                                              |
| ------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| ADFS shows "Could not create SSL/TLS secure channel" when importing metadata                           | The ADFS server does not trust MaiAgent's TLS certificate (self-signed or internal certificate) | Use "Import data from a file" instead                                                                                                 |
| Sign-in stops at "Third-Party Login Failure"                                                           | Most commonly a claim rule problem; check the server logs to confirm                            | For on-premises deployments, run `docker logs maiagent-django --since 5m 2>&1 \| grep -i saml` to view the error messages             |
| Log shows `There is no AttributeStatement on the Response`                                             | Claim rules are not configured, or the test account's AD `mail` attribute is empty              | Add the two rules per Step 4; use `Get-ADUser <account> -Properties mail` to confirm mail has a value                                 |
| Log shows `User email not found`                                                                       | The claims are issued, but the Email is not in the Name claim                                   | Check whether the claim URIs in Rule 1 match this document exactly                                                                    |
| Log shows the assertion has expired or is not yet valid (NotBefore / NotOnOrAfter)                     | The MaiAgent server and the ADFS server clocks are out of sync                                  | Configure NTP time synchronization on both hosts                                                                                      |
| Sign-in succeeds but "No Access Permission" is shown                                                   | The new member has not been assigned permissions yet (normal behavior)                          | An administrator assigns role permissions in member management                                                                        |
| After ADFS authentication succeeds, the redirect URL is missing the port and the page cannot be opened | The ACS URL in the SP metadata is missing the port (reverse proxy configuration issue)          | Fix the Nginx `proxy_set_header Host` setting as described in the hint in Step 2, regenerate the metadata, and re-import it into ADFS |

**Use SAML-tracer for debugging**: Install the SAML-tracer extension in your browser and sign in once more to see the SAML response returned by ADFS directly. If the `AttributeStatement` is empty or the Email is in the wrong claim, you can immediately confirm that the problem is in the ADFS claim rules.

## Maintenance Notes <a href="#maintenance-notes" id="maintenance-notes"></a>

* **Token-Signing certificate rollover**: By default, ADFS automatically replaces the Token-Signing certificate every year (AutoCertificateRollover). If MaiAgent is configured in IdP Metadata URL mode, simply save the authentication source again after the certificate changes to update it. If you use manual certificate mode, you must obtain the new certificate and update the settings before the rollover; otherwise, all users will be unable to sign in at that point
* **Restricting who can sign in**: After testing is complete, change the ADFS Access Control Policy from Permit everyone to specific AD groups to control which employees can sign in to MaiAgent, without changing anything on the MaiAgent side
* **Suspending SSO**: Turn off the sign-in switch for this authentication source in the MaiAgent backend; no changes are needed on the ADFS side


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.maiagent.ai/maiagent-user-guide/en/org/sso/sso-saml/sso-saml-adfs.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
