Integrating the Universal Windows platform with PlayFab
This tutorial takes you through three key points of integration between PlayFab and the Universal Windows Platform (UWP).
Note
We have created a [GitHub repository](https://github.com/PlayFab/UWPExample)
with sample code to illustrate how to integrate Universal Windows Platform with PlayFab.
Integrating the Universal Windows Platform (UWP) with PlayFab is very straightforward. There are really only three things you need to worry about:
- Linking a Windows Hello account to your PlayFab account.
- Logging in the player via Windows Hello.
- Validating a purchase made from the Windows store.
Let's take these three topics in order.
Linking a Windows Hello account to your PlayFab account
There are two ways you can link a Windows Hello account to your PlayFab account:
- You can either create a new PlayFab account using your Windows Hello credentials.
- You can link Windows Hello credentials to your existing PlayFab account.
Which one you use depends on whether or not Windows Hello is your primary authentication.
Creating a new account with Windows credentials
Before you can begin this process, you need to ask the user for their username in Windows.
Then you can follow these steps:
- Call KeyCredentialManager.RequestCreateAsync, to generate a new public key for this user.
- Call CryptographicBuffer.EncodeToBase64String, to convert the IBuffer from above to a string.
- Call
PlayFabClientAPI.RegisterWithWindowsHello
, with the following required parameters:- The Windows username.
- The Base 64-encoded public key from above.
- Assuming the register was successful, the player will now be logged in. You will get back a session token that you can use to authenticate the player with all other PlayFab APIs.
- Also, you can now use HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256) to create a hash provider, and hash the public key by calling hashProvider.HashData(publicKey).
- Convert the hashed public key to a Base 64-encoded string (CryptographicBuffer.EncodeToBase64String(publicKeyHash)) and store this string and the username in the application settings. (ApplicationData.Current.LocalSettings.Values["publicKeyHint"]). This public key hint is used to log back in.
- The username should also be stored in the user's local settings, as well for simpler login.
Linking Windows credentials to an existing PlayFab account
The process for linking Windows credentials to an existing PlayFab account is very similar. The differences are:
- First, you must already be logged into a PlayFab account. Typically this is done anonymously, using an insecure, device ID.
- Then, follow the steps in the previous section, but use
LinkWindowsHello
instead ofRegisterWithWindowsHello
. - Now the user can log in with Windows credentials in the future and access or recover their PlayFab account.
Logging in the Player via Windows Hello
Once you have linked Windows Hello credentials to a PlayFab account, you can log in with those credentials. You will need the public key hint, and the Windows username.
Follow these steps:
- Call
PlayFabClientAPI.GetWindowsHelloChallengeAsync
to create a signing challenge. - Call CryptographicBuffer.DecodeFromBase64String to create an IBuffer for the
KeyCredentialManager
to have the user sign. - Call
var retrieveResult = await
KeyCredentialManager.OpenAsync(userId) to create a key signing service. - Get the credential for this user:
var userCredential = retrieveResult.Credential
. - Call await userCredential.RequestSignAsync(challengeBuffer) to have Windows request that the user sign the server's challenge for this user.
- Finally, call
PlayFabClientAPI.LoginWithWindowsHello
to complete the process and log in the player. - Assuming the login was successful, the player is now logged in. You will get back a session token that you can use to authenticate the player with all other PlayFab APIs.
Making a purchase via Windows store
Making a purchase via the Windows store is similar to the purchase process via iOS or Android. There are two stages:
- Setting up the store in the first place.
- Then making the purchase.
Setting up the Store
Microsoft has published several documents describing this process. For example:
Setting up the in-app purchase in the Windows Store
You must also be sure to set up a matching item in the PlayFab catalog, with the same product ID.
Making the purchase
Once you have set up the items in the store, you can enable the in-app purchase in your app.
- Make the purchase in the application, and be sure to request a receipt.
- This document explains the process: making in-app purchases with UWP.
- You will get back a receipt.
- You can find more information on receipts here.
- Call PlayFabClientAPI.ValidateWindowsStoreReceipt to validate the receipt. This will automatically credit the player's inventory in PlayFab with the item they just purchased.
Further questions
If you have further questions, please post in our forums.
We'd also love to hear from you with any feedback on integrating your game with the Universal Windows Platform!