> ## 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.

# 整合通用 Windows 平台與 PlayFab

> 透過連結 Windows Hello 帳戶、登入玩家及驗證 Windows Store 購買，將通用 Windows 平台應用程式與 PlayFab 整合。

本教學課程將帶您了解 PlayFab 與通用 Windows 平台 (UWP) 之間的三個主要整合重點。

<Note>
  我們已建立一個包含範例程式碼的 [GitHub 存放庫](https://github.com/PlayFab/UWPExample)，用以說明如何整合通用 Windows 平台與 PlayFab。
</Note>

整合通用 Windows 平台 (UWP) 與 PlayFab 非常簡單。您實際上只需要注意三件事：

1. 將 Windows Hello 帳戶連結至您的 PlayFab 帳戶。
2. 透過 Windows Hello 登入玩家。
3. 驗證從 Windows 市集進行的購買。

讓我們依序說明這三個主題。

## 將 Windows Hello 帳戶連結至您的 PlayFab 帳戶

將 Windows Hello 帳戶連結至 PlayFab 帳戶的方式有兩種：

1. 您可以使用 Windows Hello 認證建立新的 PlayFab 帳戶。
2. 您可以將 Windows Hello 認證連結至現有的 PlayFab 帳戶。

要使用哪一種方式，取決於 Windows Hello 是否為您的主要驗證方式。

### 使用 Windows 認證建立新帳戶

開始此程序之前，您需要向使用者詢問其 Windows 使用者名稱。

接著您可以依照下列步驟操作：

1. 呼叫 [KeyCredentialManager.RequestCreateAsync](https://learn.microsoft.com/en-us/uwp/api/windows.security.credentials.keycredentialmanager)，為此使用者產生新的公開金鑰。
2. 呼叫 [CryptographicBuffer.EncodeToBase64String](https://learn.microsoft.com/en-us/uwp/api/Windows.Security.Cryptography.CryptographicBuffer#Windows_Security_Cryptography_CryptographicBuffer_EncodeToBase64String_Windows_Storage_Streams_IBuffer_)，將上述的 IBuffer 轉換為字串。
3. 使用下列必要參數呼叫 `PlayFabClientAPI.RegisterWithWindowsHello`\<!-- [PlayFabClientAPI.RegisterWithWindowsHello](xref:titleid.playfabapi.com.client.authentication.registerwithwindowshello) -->：
   * Windows 使用者名稱。
   * 上述的 Base 64 編碼公開金鑰。
4. 假設註冊成功，玩家現在就已登入。您會取回一個工作階段權杖，可用來向所有其他 PlayFab API 驗證玩家。
5. 此外，您現在可以使用 [HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256)](https://learn.microsoft.com/en-us/uwp/api/windows.security.cryptography.core.hashalgorithmprovider#Windows_Security_Cryptography_Core_HashAlgorithmProvider_OpenAlgorithm_System_String_) 建立雜湊提供者，並呼叫 [hashProvider.HashData(publicKey)](https://learn.microsoft.com/en-us/uwp/api/windows.security.cryptography.core.hashalgorithmprovider#Windows_Security_Cryptography_Core_HashAlgorithmProvider_HashData_Windows_Storage_Streams_IBuffer_) 對公開金鑰進行雜湊。
6. 將雜湊後的公開金鑰轉換為 Base 64 編碼字串 [(CryptographicBuffer.EncodeToBase64String(publicKeyHash))](https://learn.microsoft.com/en-us/uwp/api/Windows.Security.Cryptography.CryptographicBuffer#Windows_Security_Cryptography_CryptographicBuffer_EncodeToBase64String_Windows_Storage_Streams_IBuffer_)，並將此字串和使用者名稱儲存在應用程式設定中 (ApplicationData.Current.LocalSettings.Values\["publicKeyHint"])。此公開金鑰提示用於重新登入。
7. 使用者名稱也應儲存在使用者的本機設定中，以簡化登入。

### 將 Windows 認證連結至現有的 PlayFab 帳戶

將 Windows 認證連結至現有 PlayFab 帳戶的程序非常類似。差異如下：

1. 首先，您必須已登入 PlayFab 帳戶。這通常是以匿名方式，使用不安全的裝置識別碼完成。
2. 然後，依照上一節的步驟操作，但使用 `LinkWindowsHello`\<!-- [LinkWindowsHello](xref:titleid.playfabapi.com.client.accountmanagement.linkwindowshello) --> 取代 `RegisterWithWindowsHello`。
3. 現在，使用者日後就可以使用 Windows 認證登入，並存取或復原其 PlayFab 帳戶。

## 透過 Windows Hello 登入玩家

將 Windows Hello 認證連結至 PlayFab 帳戶後，您就可以使用這些認證登入。您將需要公開金鑰提示和 Windows 使用者名稱。

請依照下列步驟操作：

1. 呼叫 `PlayFabClientAPI.GetWindowsHelloChallengeAsync`\<!-- [PlayFabClientAPI.GetWindowsHelloChallengeAsync](xref:titleid.playfabapi.com.client.authentication.getwindowshellochallenge) --> 以建立簽署挑戰。
2. 呼叫 [CryptographicBuffer.DecodeFromBase64String](https://learn.microsoft.com/en-us/uwp/api/Windows.Security.Cryptography.CryptographicBuffer#Windows_Security_Cryptography_CryptographicBuffer_DecodeFromBase64String_System_String_)，為 `KeyCredentialManager` 建立 IBuffer，讓使用者進行簽署。
3. 呼叫 `var retrieveResult = await` [KeyCredentialManager.OpenAsync(userId)](https://learn.microsoft.com/en-us/uwp/api/windows.security.credentials.keycredentialmanager#Windows_Security_Credentials_KeyCredentialManager_OpenAsync_System_String_) 以建立金鑰簽署服務。
4. 取得此使用者的認證：`var userCredential = retrieveResult.Credential`。
5. 呼叫 await [userCredential.RequestSignAsync(challengeBuffer)](https://learn.microsoft.com/en-us/uwp/api/Windows.Security.Credentials.KeyCredential#Windows_Security_Credentials_KeyCredential_RequestSignAsync_Windows_Storage_Streams_IBuffer_)，讓 Windows 要求使用者為此使用者簽署伺服器的挑戰。
6. 最後，呼叫 `PlayFabClientAPI.LoginWithWindowsHello`\<!-- [PlayFabClientAPI.LoginWithWindowsHello](xref:titleid.playfabapi.com.client.authentication.loginwithwindowshello) --> 以完成程序並登入玩家。
7. 假設登入成功，玩家現在就已登入。您會取回一個工作階段權杖，可用來向所有其他 PlayFab API 驗證玩家。

## 透過 Windows 市集進行購買

透過 Windows 市集進行購買，與透過 iOS 或 Android 的購買程序類似。共有兩個階段：

1. 首先設定市集。
2. 然後進行購買。

### 設定市集

Microsoft 已發佈數份說明此程序的文件。例如：

[在 Windows 市集中設定應用程式內購買](https://learn.microsoft.com/en-us/windows/uwp/publish/add-on-submissions)

您也必須確保在 PlayFab 目錄中設定具有相同產品識別碼的對應項目。

### 進行購買

在市集中設定好項目後，您就可以在應用程式中啟用應用程式內購買。

1. 在應用程式中進行購買，並務必要求收據。
   * 此文件說明了該程序：[使用 UWP 進行應用程式內購買](https://learn.microsoft.com/en-us/uwp/api/windows.applicationmodel.store.currentapp#Windows_ApplicationModel_Store_CurrentApp_RequestProductPurchaseAsync_System_String_System_Boolean_)。
2. 您會取回一張收據。
   * 您可以在[這裡找到更多關於收據的資訊](https://learn.microsoft.com/en-us/windows/uwp/monetize/use-receipts-to-verify-product-purchases)。
3. 呼叫 [PlayFabClientAPI.ValidateWindowsStoreReceipt](xref:titleid.playfabapi.com.client.platformspecificmethods.validatewindowsstorereceipt) 以驗證收據。這會自動將玩家剛購買的項目存入其在 PlayFab 中的庫存。

## 其他問題

如果您有其他問題，請在我們的[論壇](https://community.playfab.com/index.html)中發文。

我們也很樂意聽取您對將遊戲與通用 Windows 平台整合的任何意見反應！


## Related topics

- [平台特定驗證](/zh-TW/services/playfab/identity/player-identity/platform-specific-authentication/index.md)
- [適用於 Windows 的 PlayFab Services](/zh-TW/services/playfab/sdks/platforms/windows.md)
- [XBOX 服務遊戲的 PlayFab 整合](/zh-TW/services/xbox-services/playfab-integration.md)
- [適用於 GDK 的 PlayFab Services](/zh-TW/services/playfab/sdks/platforms/gdk.md)
- [市集整合 - 概觀](/zh-TW/services/playfab/economy-monetization/economy-v2/marketplace/marketplace-integrations/overview.md)
