> ## 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 应用商店购买，将通用 Windows 平台应用与 PlayFab 集成。

本教程将引导你了解 PlayFab 与通用 Windows 平台 (UWP) 之间的三个关键集成点。

> [!NOTE]
>
> 我们创建了一个包含示例代码的 [GitHub 存储库](https://github.com/PlayFab/UWPExample)，用于说明如何将通用 Windows 平台与 PlayFab 集成。

将通用 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 帐户。通常这是使用不安全的设备 ID 以匿名方式完成的。
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_) 创建一个 IBuffer，供 `KeyCredentialManager` 让用户签名。
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 目录中设置具有相同产品 ID 的匹配项。

### 进行购买

在应用商店中设置项目后，即可在应用中启用应用内购买。

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-CN/services/playfab/identity/player-identity/platform-specific-authentication/index.md)
- [使用通用 Windows 平台设置 PlayFab 身份验证](/zh-CN/services/playfab/identity/player-identity/platform-specific-authentication/uwp.md)
- [适用于 XBOX 服务游戏的 PlayFab 集成](/zh-CN/services/xbox-services/playfab-integration.md)
- [市场集成 - 概览](/zh-CN/services/playfab/economy-monetization/economy-v2/marketplace/marketplace-integrations/overview.md)
- [Economy 版本 2 (V2) 常见问题解答](/zh-CN/services/playfab/economy-monetization/economy-v2/faq.md)
