> ## 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 授權系統會執行授權檢查。除非使用者擁有產品的有效授權 (包括直接擁有權、[共用授權](/zh-TW/publishing/xstore-commerce/xstore-product-sharing)或光碟授權 (僅限 XBOX 主機))，否則作業系統不會啟動遊戲。不過，在 PC 上，使用者取得有效授權後，可以輕易解密內容、在 Microsoft Store 環境之外執行內容、與他人共用內容，並略過 Microsoft Store 授權系統。因此，我們建議想要防範此行為的任何 PC 遊戲都加入基本授權檢查。

## 數位版權管理 (DRM) 保護層級

PC 上的 Microsoft Store 授權系統只能防範最基本的攻擊。對於需要保護的內容，我們建議您不要只依賴它。以下是可針對更進階的攻擊增加保護層的幾個步驟。

1. 在遊戲啟動時由遊戲起始的授權檢查
2. 遊戲授權檢查的防竄改保護
3. 使用[授權權杖](/zh-TW/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](/zh-TW/reference/system/xstore/structs/xstoregamelicense) 結構不包含可識別授權取得方式的欄位，因此用戶端授權檢查無法區分 Game Pass 存取和直接購買。若要做出此區分，請使用[從您的服務偵測 XBOX Game Pass 訂閱存取](/zh-TW/publishing/xstore-commerce/xstore-detecting-game-pass)中所述的服務端查詢。
</Note>

### 非 Microsoft 防竄改保護

雖然基本授權檢查範例提供了一些保護，但攻擊者常會修改遊戲的可執行檔來略過此類檢查。因此，保護遊戲的下一步是使用非 Microsoft 的防竄改產品來防止這類攻擊。GDK PC 遊戲有可用的選項。請與您的防竄改廠商合作，取得如何將此保護納入遊戲的詳細指示。

### 服務端授權驗證

防竄改保護在遊戲發行時能有效保護遊戲，但最終其中許多保護都會被破解。若要更進一步，支援線上功能的遊戲可以執行服務端授權驗證檢查，並阻擋許多用戶端攻擊。如需詳細資訊，請參閱[授權權杖概觀](/zh-TW/publishing/xstore-commerce/xstore-license-tokens)。

基本概要如下。

1. 遊戲服務向遊戲用戶端要求授權權杖，並包含由遊戲服務產生、代表該唯一要求的 GUID。
2. 用戶端接著呼叫用戶端 API [XStoreQueryLicenseTokenAsync](/zh-TW/reference/system/xstore/xstore_members)。
3. API 會呼叫授權服務，並判斷遊戲是否應將授權傳回給該使用者。
4. 如果 API 取得有效授權，就會傳回包含服務提供之工作階段識別碼的已簽署授權權杖。
5. 用戶端接著將授權權杖傳回給遊戲服務。
6. 遊戲服務驗證簽章，確保授權來自 Microsoft Store 且未經修改。
7. 遊戲服務接著檢查工作階段 GUID 是否與步驟 1 中傳遞給用戶端的 GUID 相符。
8. 遊戲服務接著便可信任權杖中的授權資訊是有效的。

此技術僅適用於線上，且需要遊戲擁有自己的服務。它提供目前可用的最高層級保護。

## 參考 API 文件

* [XStore (API 內容)](/zh-TW/reference/system/xstore/xstore_members)
  * 函式
    * [XStoreQueryLicenseTokenAsync](/zh-TW/reference/system/xstore/xstore_members)

## 參考資料

[PC 遊戲專屬的商務資訊](/zh-TW/publishing/xstore-commerce/xstore-pc)

[遊戲的產品共用模型](/zh-TW/publishing/xstore-commerce/xstore-product-sharing)

[授權權杖概觀](/zh-TW/publishing/xstore-commerce/xstore-license-tokens)

[商務概觀](/zh-TW/publishing/xstore-commerce/xstore-commerce-overview)

[XStore API 參考](/zh-TW/reference/system/xstore/xstore_members)


## Related topics

- [PC 上的 XStore：DRM、授權權杖和沙箱](/zh-TW/publishing/xstore-commerce/xstore-pc.md)
- [PC GDK 遊戲的模組支援](/zh-TW/build/core-features/common/packaging/packaging-mods.md)
- [XStoreQueryLicenseTokenAsync](/zh-TW/reference/system/xstore/functions/xstorequerylicensetokenasync.md)
- [使用者驗證與擁有權](/zh-TW/build/steam-porting-guide/features/user-authentication-and-ownership.md)
- [即時活動服務概觀](/zh-TW/services/xbox-services/fundamentals/rta/live-real-time-activity-service-overview.md)
