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

# 基础 DRM 与许可证检查

> 概述 PC 游戏为防止盗版并确保游戏在有效许可证下运行应采取的注意事项和预防措施。

当通过 Microsoft Store 分发的游戏启动时，Microsoft Store 授权系统会执行许可证检查。除非用户对该产品拥有有效许可证（包括直接拥有、[共享许可证](/publishing/xstore-commerce/xstore-product-sharing)或光盘许可证（仅限 XBOX 主机）），否则操作系统不会启动游戏。然而，在 PC 上，一旦用户拥有了有效许可证，他们就可以轻易解密内容，脱离 Microsoft Store 的上下文运行、与他人共享并绕过 Microsoft Store 授权系统。因此，我们建议为所有希望防范此类行为的 PC 游戏添加基本的许可证检查。

## 数字版权管理 (DRM) 保护的等级

PC 上的 Microsoft Store 授权系统只能防御最基本的攻击。我们建议你不要仅依赖它来保护需要保护的内容。以下几项措施可以为更高级的攻击增加保护层。

1. 游戏在启动时发起的许可证检查
2. 针对游戏许可证检查的防篡改保护
3. 使用[许可证令牌](/publishing/xstore-commerce/xstore-license-tokens)进行的服务端许可证验证

### 基础许可证检查

作为最基本要求，我们建议为任何希望获得 DRM 保护的游戏添加基础许可证检查。以下代码示例展示了执行此检查的基础实现，即使你没有使用任何其他 Gaming Runtime 相关的 API 调用也可以使用。你可以将此代码添加到启动路径中，并在许可证检查失败时显示特定的 UI。

```cpp theme={null}
void CALLBACK LicenseCheck(XAsyncBlock *async)
{
    XStoreGameLicense license;
    HRESULT hr = XStoreQueryGameLicenseResult(async, &license);
    if (SUCCEEDED(hr))
    {
        if (!license.isActive)
        {
            // Failed to retrieve a license
            // Consider the title to be unlicensed
        }
        else
        {
            if (license.isTrial)
            {
                // Validated that the user has a trial license
                // Check additional attributes to ensure active trial
                // See "Implementing Trials for your game"
            }
            else
            {
                // Validated that the user has a real license
                // Consider the title to be licensed and continue
            }
        }
    }
    else
    {
        // Failed to retrieve a license
        // Consider the title to be unlicensed
    }

    delete async;
}

void CheckLicense()
{
    auto async = new XAsyncBlock{};
    async->context = this;
    async->queue = m_asyncQueue;
    async->callback = LicenseCheck;

    HRESULT hr = XStoreQueryGameLicenseAsync(
        storeContext,
        async);

    if (FAILED(hr))
    {
        // StoreContext may be invalid
        // If loose deployed or side-loaded package, check that the game configured to be licensed
        // See "Enabling XStore development and testing"

        delete async;
    }
}
```

<Note>
  通过 XBOX Game Pass 授予的许可证会返回 `isActive = true`，与购买获得的许可证相同。[XStoreGameLicense](/reference/system/xstore/structs/xstoregamelicense) 结构体中没有标识许可证获取方式的字段，因此客户端许可证检查无法区分 Game Pass 访问与直接购买。若要进行这种区分，请使用[从你的服务检测 XBOX Game Pass 订阅访问](/publishing/xstore-commerce/xstore-detecting-game-pass)中介绍的服务端查询。
</Note>

### 非 Microsoft 提供的防篡改保护

尽管基础许可证检查示例提供了一定的保护，但攻击者常常会修改游戏的可执行文件来绕过此类检查。因此，保护游戏的下一步是使用非 Microsoft 提供的防篡改产品来阻断这些攻击途径。GDK PC 游戏有可选方案。请与你的防篡改供应商合作，获取如何将此保护集成到游戏中的详细说明。

### 服务端许可证验证

防篡改保护在游戏启动时能起到很好的保护作用，但大多数最终都会被绕过。若要更进一步，联网游戏可以执行服务端许可证验证检查，并阻止许多客户端攻击。有关更多信息，请参阅[许可证令牌概述](/publishing/xstore-commerce/xstore-license-tokens)。

基本流程如下：

1. 游戏服务向游戏客户端请求许可证令牌，并附上一个由游戏服务生成、代表该唯一请求的 GUID。
2. 客户端随后调用客户端 API [XStoreQueryLicenseTokenAsync](/reference/system/xstore/xstore_members)。
3. 该 API 调用授权服务，并判断游戏是否应向该用户返回许可证。
4. 如果 API 能获得有效许可证，则返回一个已签名的许可证令牌，其中包含服务端提供的会话标识符。
5. 客户端随后将许可证令牌回传给游戏服务。
6. 游戏服务验证签名，确认该许可证来自 Microsoft Store 且未被修改。
7. 游戏服务随后核对会话 GUID 是否与第 1 步中传给客户端的一致。
8. 之后游戏服务可以信任该令牌中的授权信息是有效的。

此技术仅在联网时有效，且要求游戏拥有自己的服务。它提供目前可用的最高级别保护。

## 参考 API 文档

* [XStore（API 内容）](/reference/system/xstore/xstore_members)
  * 函数
    * [XStoreQueryLicenseTokenAsync](/reference/system/xstore/xstore_members)

## 参考资料

[面向 PC 游戏的商务信息](/publishing/xstore-commerce/xstore-pc)

[游戏的产品共享模式](/publishing/xstore-commerce/xstore-product-sharing)

[许可证令牌概述](/publishing/xstore-commerce/xstore-license-tokens)

[商务概述](/publishing/xstore-commerce/xstore-commerce-overview)

[XStore API 参考](/reference/system/xstore/xstore_members)


## Related topics

- [PC 上的 XStore — DRM、许可证令牌和沙盒](/zh-CN/publishing/xstore-commerce/xstore-pc.md)
- [用户身份验证与所有权](/zh-CN/build/steam-porting-guide/features/user-authentication-and-ownership.md)
- [从你的服务检测 XBOX Game Pass 订阅访问](/zh-CN/publishing/xstore-commerce/xstore-detecting-game-pass.md)
- [XStoreQueryLicenseTokenAsync](/zh-CN/reference/system/xstore/functions/xstorequerylicensetokenasync.md)
- [PC GDK 游戏的模组支持](/zh-CN/build/core-features/common/packaging/packaging-mods.md)
