Did you find this helpful?
Integrating UWP with PlayFab
Integrating UWP with PlayFab
NOTE: We have created a GitHub repository with sample code to illustrate how to integrate Universal Windows Platform with PlayFab.
Integrating the Universal Windows Platform 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, or you can link Windows Hello credentials to your existing PlayFab account. Which one to use depends on whether Windows Hello is your primary authentication, or not.
Creating a new account with Windows credentials
Before you can begin this process, you need ask the user for their username under 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 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 user name 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, you would follow the above steps, but use LinkWindowsHello instead of RegisterWithWindowsHello.
- Now the user can login 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 login 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 the user sign the server's challenge for this user
- Finally you can call PlayFabClientAPI.LoginWithWindowsHello to complete the process and login the player
- Assuming 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 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:
You need to 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. For example 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.
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!