# 设置 Azure AD

{% hint style="warning" %}
本指南将带你完成为你的文档设置受保护的登录界面的过程。在继续本指南之前，请确保你已经先完成了 [启用经过身份验证的访问](https://gitbook-v2-q67etdj25-gitbook.vercel.app/url/gitbook.com/docs/documentation/zh/publishing-documentation/authenticated-access/enabling-authenticated-access).
{% endhint %}

{% hint style="info" %}
Azure 集成存在一个已知限制：在身份验证后，标题 URL 片段会被移除。用户仍会进入正确的页面，但会被带到页面顶部，而不是 URL 中指定的标题处。一旦用户完成身份验证，在该会话期间就不会再出现这种行为，用户也会被定向到正确的标题。

这是由于微软实施的一项安全措施。
{% endhint %}

### 概述

要使用 Azure AD 为你的 GitBook 站点配置已验证访问，流程如下：

{% stepper %}
{% step %}
[**在 Azure AD 中创建应用注册**](#id-1.-create-an-app-registration-in-azure-a-d)

在你的 Microsoft Entra ID 管理控制台中创建一个 Azure AD 应用注册。
{% endstep %}

{% step %}
[**在你的站点上安装并配置 Azure AD 集成**](#id-2.-install-and-configure-the-azure-a-d-integration)

安装 Azure AD 集成，并将所需配置添加到你的 GitBook 站点。
{% endstep %}

{% step %}
[**为自适应内容配置 Azure AD（可选）**](#id-3.-configure-azure-a-d-for-adaptive-content-optional)

配置你的 Azure AD 以便在 GitBook 中与自适应内容配合使用。
{% endstep %}
{% endstepper %}

### 1. 在 Azure AD 中创建应用注册

首先在你的 Microsoft Entra ID 控制台中创建一个应用注册。此应用注册将允许 GitBook Azure AD 集成请求令牌，以在授予用户访问你的网站之前验证其身份。

1. 登录到你的 Microsoft Entra ID 管理员 [控制台](https://entra.microsoft.com/).
2. 前往 **身份** > **应用程序** > **应用注册** ，位于左侧边栏。
3. 点击 **+ 新注册，** 并为你的注册命名。
4. 在 **支持的帐户类型，** 选择“**此组织目录中的帐户（仅默认目录 - 单租户）”**.
5. 先将重定向 URI 字段留空——稍后你需要填写此项。
6. 点击 **注册** 以完成应用注册。<br>

   <figure><img src="https://2111890564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNkEGS7hzeqa35sMXQZ4X%2Fuploads%2FuB6p6HxRmveHp1cfsiKa%2Fazure_ad_integration_register_app.png?alt=media&#x26;token=cbc82bc5-189e-433a-b990-042066768307" alt="An Azure screenshot showing how to register an Azure AD app"><figcaption><p>为 GitBook VA 集成注册一个应用。</p></figcaption></figure>
7. 然后你应该会看到新的应用注册 **概述** 页面。复制并记下 **应用程序（客户端）ID** 并在 **目录（租户）ID**.<br>

   <figure><img src="https://2111890564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNkEGS7hzeqa35sMXQZ4X%2Fuploads%2FETQziXVuqGnho8GGOVBZ%2Fazure_ad_integration_app_reg_overview.png?alt=media&#x26;token=266a55e3-db75-4a4a-923c-8302f69653d5" alt="An Azure screenshot showing the app registration overview"><figcaption><p>新创建的应用注册概览。</p></figcaption></figure>
8. 点击 **添加证书或密码**。你应该会看到以下 **证书和密码** 页面：<br>

   <figure><img src="https://2111890564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNkEGS7hzeqa35sMXQZ4X%2Fuploads%2FnXJbnpnGhOI6Udjrx8bS%2Fazure_ad_integration_client_secrets.png?alt=media&#x26;token=9202689b-4657-4bea-bf74-42b51e1051e5" alt="An Azure screenshot showing where to add a certificate or secret"><figcaption><p>添加证书或密码。</p></figcaption></figure>
9. 点击 **+ 新建客户端密码**.
10. 为该密码输入合适的描述，然后点击 **添加**.
11. 复制并记下 **值** 字段（***不** 该密码的机密 ID*）。

### 2. 安装并配置 Azure AD 集成

创建 Azure AD 应用注册后，下一步是在 GitBook 中安装 Azure AD 集成，并使用你之前生成的凭据将其与你的 Azure 应用链接起来：

1. 导航到你已 [启用已验证访问](https://gitbook-v2-q67etdj25-gitbook.vercel.app/url/gitbook.com/docs/documentation/zh/publishing-documentation/enabling-authenticated-access#enable-authenticated-access) 并希望将 Azure AD 作为身份提供方的站点。
2. 点击你站点设置右上角的 **集成** 按钮。<br>

   <figure><img src="https://2111890564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNkEGS7hzeqa35sMXQZ4X%2Fuploads%2FgBMsbydNsuqx7eqcz0JN%2Fva_site_integration_overview_screen.png?alt=media&#x26;token=ead70cf3-93cf-4aef-b77a-321afad38900" alt="A GitBook screenshot showing the site settings overview"><figcaption></figcaption></figure>
3. 点击 **已认证访问** 在侧边栏的分类中。
4. 选择 **Azure** 集成。
5. 点击 **安装到此站点**.<br>

   <figure><img src="https://2111890564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNkEGS7hzeqa35sMXQZ4X%2Fuploads%2FCiveWUn0FWMEdS3YRU62%2FScreenshot%202025-03-24%20at%2018.10.09.png?alt=media&#x26;token=fcaedce7-40f5-40e5-acd1-040e90822be4" alt="A GitBook screenshot showing installation of the Azure AD integration"><figcaption></figcaption></figure>
6. 在你的站点上安装集成后，你应该会看到该集成的配置界面：<br>

   <figure><img src="https://2111890564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNkEGS7hzeqa35sMXQZ4X%2Fuploads%2FvO9pXBCZkcprDDIVFkcf%2FScreenshot%202025-03-24%20at%2018.17.02.png?alt=media&#x26;token=e082d3dd-1e6b-44b4-beaf-a5d67812e055" alt="A GitBook screenshot showing the Azure AD configuration dialog"><figcaption></figcaption></figure>
7. 输入你之前在 **客户端 ID**, **租户 ID**，以及 **客户端密钥** 值， [即你之前创建 Azure AD 应用注册后复制的内容](#id-1.-create-an-app-registration-in-azure-a-d) ，然后点击“保存”。
8. 复制 **URL** 显示的 **在对话框底部**.
9. 返回到你之前在 Microsoft Entra ID 控制台中创建的 Azure AD 应用注册。
10. 浏览到 **管理** > **身份验证** 在侧边栏中。
11. 点击 **+ 添加平台** 并选择 **Web** 卡片，在打开的面板中。<br>

    <figure><img src="https://2111890564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNkEGS7hzeqa35sMXQZ4X%2Fuploads%2FUssw9BNY66urLYH376sE%2FScreenshot%202025-03-24%20at%2018.28.58.png?alt=media&#x26;token=2c2b2b84-b7e9-4fc3-a7bd-2751dacfd579" alt="An Azure screenshot showing authentication platform settings"><figcaption></figcaption></figure>
12. 粘贴你之前复制的 GitBook 集成 **URL** 到 **重定向 URI** 字段中，然后点击“配置”<br>

    <figure><img src="https://2111890564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNkEGS7hzeqa35sMXQZ4X%2Fuploads%2Fi9fVjsa351I6kVzJw64X%2Fimage.png?alt=media&#x26;token=85d1d25a-bdb8-4453-b30e-54a21304f870" alt="An Azure screenshot showing where to enter the redirect URI"><figcaption></figcaption></figure>
13. 返回到 **Azure 集成** 安装页面 **在 GitBook 中**.
14. 关闭集成对话框，然后点击站点页面中的 **设置** 选项卡。
15. 浏览到 **受众** 并选择 **已认证访问** （如果尚未选中）。
16. 选择 **Azure** 从下拉菜单中 **身份验证后端** 部分。
17. 点击 **更新受众**.<br>

    <figure><img src="https://2111890564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNkEGS7hzeqa35sMXQZ4X%2Fuploads%2Finc5DrItbyXUIXP4K4ic%2FScreenshot%202025-03-24%20at%2018.41.45.png?alt=media&#x26;token=e06283f6-f7d6-4556-9ef7-747b47493064" alt="A GitBook screenshot showing authenticated access settings"><figcaption></figcaption></figure>
18. 前往站点概览页面并点击 **发布** 如果站点尚未发布。

你的站点现在已通过 Azure AD 作为身份提供方，在已验证访问后方发布。

要进行测试，请点击访问。系统会要求你使用 Azure 登录，这将确认你的站点已通过 Azure 进行已验证访问后发布。

{% hint style="info" %}
在访问已发布内容 URL 并使用你的 Azure 凭据登录后，你可能会看到一个页面，提示你需要向管理员“请求批准”。你的管理员可以通过访问已发布内容 URL、登录，并代表组织授予批准来完成此请求。
{% endhint %}

### 3. 为自适应内容配置 Azure AD（可选）

要在已验证访问站点中利用自适应内容功能，请配置 Azure AD 应用注册，使身份验证令牌中以声明（claims）的形式包含额外的用户信息。

这些声明以键值对形式表示，会传递给 GitBook，并可用于 [自适应内容](https://gitbook-v2-q67etdj25-gitbook.vercel.app/url/gitbook.com/docs/documentation/zh/publishing-documentation/adaptive-content/adapting-your-content) ，以动态方式服务于你站点的访客。

Azure AD 支持不同类型和级别的声明，每种声明都有各自的设置方法：

* **标准声明**：可能包含在令牌中的常见声明，但默认情况下并不总是存在。

{% hint style="info" %}
Azure AD 会优化令牌大小以提升性能。因此，许多声明 **不** 默认包含在令牌中，必须由应用显式请求。为确保诸如 `电子邮件` , `组` 或者 `角色` 之类的声明被包含在内，必须将它们显式请求为 **可选声明**.
{% endhint %}

* **可选声明**：可为应用启用的额外预定义声明。
* **自定义声明**：来自 Azure AD 中自定义用户属性或通过自定义声明提供程序从外部系统获取的声明。

有关如何将这些不同类型的声明包含到 Azure AD 应用生成的令牌中的更多详情，请参阅以下 Microsoft Entra 文档指南：

* [用户属性](https://learn.microsoft.com/en-us/entra/external-id/customers/how-to-add-attributes-to-token)
* [可选声明](https://learn.microsoft.com/en-us/entra/identity-platform/optional-claims?toc=%2Fentra%2Fexternal-id%2Ftoc.json\&bc=%2Fentra%2Fexternal-id%2Fbreadcrumb%2Ftoc.json\&tabs=appui)
* [自定义声明](https://learn.microsoft.com/en-us/entra/identity-platform/custom-claims-provider-overview)

在设置并配置好要发送给 GitBook 的正确 claims 后，前往“[调整你的内容](https://gitbook-v2-q67etdj25-gitbook.vercel.app/url/gitbook.com/docs/documentation/zh/publishing-documentation/adaptive-content/adapting-your-content)”以继续配置你的网站。
