다음을 통해 공유


Azure PlayFab: Authentication

Introduction

Microsoft Azure PlayFab is a complete backend platform for live games supporting a large range of features for modern game development. The previous TechNet Wiki provided an overview of the features of PlayFab in order to raise awareness of this excellent platform. This article provides a look into PlayFab support for authentication.

PlayFab Authentication

One of the great features of PlayFab is the comprehensive documentation and PlayFab has Authentication documented for both Player Login and Platform-Specific Authentication.

PlayFab breaks Player login into two main categories: Anonymous login mechanisms and Recoverable login mechanisms. Anonymous login requires no input from the user and provides a seamless experience. This is accomplished by uniquely identifying the device but there is no recoverable information. This means if the device is lost or damaged, there is no mechanism for recovering. Recoverable login mechanisms require some form of identity provider like Facebook, iOS, Google, PlayStation, Steam, Kongregate, and Xbox Live. There is also the option of using PlayFab as an identity provider.

Be sure to have a look at the Login basics and best practices guide for some great advice on setting up authentication. 

PlayFab developer account

Before you can call the PlayFab API, you need an account. This is pretty painless as there is a free account option available. Please follow the setup instructions provided by PlayFab: Signup.

C# Quickstart

This wiki builds upon the steps in the quickstart. Have a look at the documentation if you have any questions or issues but here is a short summary of the quickstart:

  1. Create a console application using .Net Standard:

  2. Install the PlayFabAllSDK using NuGet:

  3. The code provided in the quickstart simply logs into PlayFab. Let's look at this in some detail. 

First, the general flow is below

static void  Main(string[] args)
{
    // set the id of the title that we will authenticate against        
    ...
     
    // submit a request to login and wait until it completes        
    ...
     
    Console.WriteLine("Done! Press any key to close");
    Console.ReadKey(); // This halts the program and waits for the user
}

Now let's look at each section. The first step is simple and only requires the id of the title to be set:

// set the id of the title that we will authenticate against
PlayFabSettings.staticSettings.TitleId = "9BB50";

This value can be found on the summary of the titles on My Studio and Titles page:

The request for login is a little more interesting:

// submit a request to login        
PlayFabClientAPI.LoginWithCustomIDAsync(new LoginWithCustomIDRequest 
                           { 
                               CustomId = "UniqueValueForThisDevice", 
                               CreateAccount = true
                           })    
           .ContinueWith(OnLoginComplete)
           .Wait();

The snippet above calls the API and supplies a unique id to identify the device and flag to indicate that an account should be created if one is missing. This is an example of anonymous as the user does not have to supply any information.

The method OnLoginComplete is called when the API completes:

private static  void OnLoginComplete(Task<PlayFabResult<LoginResult>> taskResult)
{
    if (taskResult.Result.Error != null)
    {
        Console.ForegroundColor = ConsoleColor.Red; 
        Console.WriteLine("Failure calling PlayFab Authentication");                
        Console.WriteLine(PlayFabUtil.GenerateErrorReport(taskResult.Result.Error));
        Console.ForegroundColor = ConsoleColor.Gray; 
    }
    else
    if (taskResult.Result.Result != null)
    {
        Console.WriteLine("Congratulations, you made your first successful API call!");
    }            
}

The snippet above simply writes an error message or success based on the response from PlayFab.

PlayFab Dashboard

If we look at the PlayFab dashboard for the game we can see a single API call having been made:

PlayFab Players

And looking at the Players section we can see that one player has been created today:

Let's collect some more information about our player. This is done by submitting in a dictionary of values using the UpdateUserData API:

var data = new  Dictionary<string, string>();
data.Add("Points", "100");
data.Add("Region", "Somewhere in the Pacific Ocean");
 
PlayFabClientAPI.UpdateUserDataAsync(new UpdateUserDataRequest { Data = data });

The UpdateUserData API uploads custom data and the data can be found in the PlayFab portal now associated with the user as shown below:

Additional Identities

Now that we have an idea of how to create an account and collect some data, let's say in the game we give the user the option to create a PlayFab account. We would then require the user to enter in a username, email, and password. These values would then be submitted by using the AddUsernamePassword API:

PlayFabClientAPI.AddUsernamePasswordAsync(new AddUsernamePasswordRequest
                            {
                                  Username = "SummerSquash",
                                  Email = "chilberto@wherever.com",
                                  Password = "TickityB00!"
                             })
            .ContinueWith(OnAddUsernamePasswordCompleted)
            .Wait();

In the PlayFab Player view after the AddUsernamePassword API has been executed, we can now the PlayFab username and login email has been updated:

This then allows for a different type of user experience where the user now has the option of entering in a username and password in order to gain access using LoginWithEmailAddress API:

PlayFabClientAPI.LoginWithEmailAddressAsync(new LoginWithEmailAddressRequest 
                { 
                   Email = args[0], 
                   Password = args[1] 
                })
           .ContinueWith(OnLoginComplete)
           .Wait();

Summary

The aim of this article is to further explore another aspect of the Azure PlayFab service. An important aspect of backend game services is identity management. This article shows how simple anonymous login is to achieve and how this can be expanded to include username/password authentication. Likewise adding additional identity allows users greater flexibility to enjoy a game their way.

For example, some users might use an Android phone during the day and an iPad at night. PlayFab supports this scenario by allowing the same user to be authenticated using different mechanisms. This article illustrated this by using the CustomId and Email login APIs and additional APIs are shown below:

  • LoginWithCustomIDAsync
  • LoginWithAndroidDeviceIDAsync
  • LoginWithFacebookAsync
  • LoginWithFacebookInstantGamesIdAsync
  • LoginWithGameCenterAsync
  • LoginWithGoogleAccountAsync
  • LoginWithIOSDeviceIDAsync
  • LoginWithKongregateAsync
  • LoginWithNintendoSwitchDeviceIdAsync
  • LoginWithOpenIdConnectAsync
  • LoginWithPlayFabAsync
  • LoginWithPSNAsync
  • LoginWithSteamAsync
  • LoginWithTwitchAsync
  • LoginWithWindowsHelloAsync
  • LoginWithXboxAsync

Please find the example source here: https://github.com/chilberto/TechNetWikiPlayFabAuthentication

See Also

The following article are recommended for next reading