> ## 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) の統合における 3 つの重要なポイントについて説明します。

> [!NOTE]
>
> ユニバーサル Windows プラットフォームを PlayFab と統合する方法を示すサンプル コードを含む [GitHub リポジトリ](https://github.com/PlayFab/UWPExample) を作成しました。

ユニバーサル Windows プラットフォーム (UWP) と PlayFab の統合は非常に簡単です。考慮する必要があるのは、実際には次の 3 つだけです:

1. Windows Hello アカウントを PlayFab アカウントにリンクする。
2. Windows Hello を使用してプレイヤーをログインさせる。
3. Windows ストアで行われた購入を検証する。

これら 3 つのトピックを順番に見ていきましょう。

## Windows Hello アカウントを PlayFab アカウントにリンクする

Windows Hello アカウントを PlayFab アカウントにリンクするには、次の 2 つの方法があります:

1. Windows Hello の資格情報を使用して新しい PlayFab アカウントを作成する。
2. 既存の PlayFab アカウントに Windows Hello の資格情報をリンクする。

どちらを使用するかは、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. ログインを簡単にするために、ユーザー名もユーザーのローカル設定に保存する必要があります。

### 既存の PlayFab アカウントに Windows 資格情報をリンクする

既存の PlayFab アカウントに Windows 資格情報をリンクするプロセスも非常によく似ています。違いは次のとおりです:

1. まず、既に PlayFab アカウントにログインしている必要があります。通常、これはセキュリティで保護されていないデバイス ID を使用して匿名で行われます。
2. 次に、前のセクションの手順に従いますが、`RegisterWithWindowsHello` の代わりに `LinkWindowsHello`\<!-- [LinkWindowsHello](xref:titleid.playfabapi.com.client.accountmanagement.linkwindowshello) --> を使用します。
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 での購入プロセスに似ています。次の 2 つの段階があります:

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

- [ユニバーサル Windows プラットフォームを使用した PlayFab 認証のセットアップ](https://devdocs.xbox.com/ja-jp/services/playfab/identity/player-identity/platform-specific-authentication/uwp.md)
- [Game Chat 2 の概要](https://devdocs.xbox.com/ja-jp/services/xbox-services/multiplayer/chat/game-chat2/game-chat-2-intro.md)
- [PlayFab がサポートするプラットフォーム](https://devdocs.xbox.com/ja-jp/services/playfab/sdks/platforms/index.md)
- [XPackageUninstallUWPInstance](https://devdocs.xbox.com/ja-jp/reference/system/xpackage/functions/xpackageuninstalluwpinstance.md)
- [XblMultiplayerActivityPlatform](https://devdocs.xbox.com/ja-jp/reference/live/xsapi-c/multiplayer_activity_c/enums/xblmultiplayeractivityplatform.md)
