LoginWithCustomID in C# Console

Learn how to use the PlayFab Client library for C# on the LoginWithCustomID authentication mechanism.

As part of this guide, We will first perform some tests with a basic structure, then implement LoginWithCustomID to Unity and at least perform the same in an MVC ASP.Net Core Website.

You can retrieve the full code on YAKAGameStudio (github.com)

Prerequisites

Before doing this tutorial, make sure you have:

Create a C# Console with PlayFab SDK

This tutorial provides the minimum steps required to make your first PlayFab API call. Confirmation is done via a console print.

  1. Open Visual Studio and Select Create a new project.
  2. Select Console App (.Net Core) for C#.
  3. Install NuGet package for PlayFabAllSDK. For instructions, see Install and use a package in Visual Studio.
Visual Studio NuGet Package Manager
Install PlayFab SDK with NuGet Package

At this point, you should be able to successfully compile the project. The output window should contain something like the following example.

1>------ Build started: Project: ConsoleAuthentication, Configuration: Debug Any CPU ------
1>  ConsoleAuthentication-> c:\Learn\Authentication\Console\bin\Debug\ConsoleAuthentication.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Set up your LoginWithCustomID Authentication Call

Your new project contain a file called Program.cs, which was created automatically by Visual Studio. Open that file, and add the code in the example shown below. You want to go through the final step ? check this file on GitHub

For this demo, all code lines will stay into the Program.cs file. We will see later how to structure the authentication class

Setup PlayFab Title

PlayFab API is scoped by game. When the call is initiated, the request is sent to: https://titleId.playfabapi.com/Client/LoginWithCustomID. So you need to setup the Game Title you are working on.

static void Main(string[] args)
{
	// Provide your titleId from PlayFab Game Manager
    PlayFabSettings.staticSettings.TitleId = "titleId";
    // Wait for the user to respond before closing.
    Console.WriteLine("Done! Press any key to close");
    Console.ReadKey();
}

Initiate request header

When initiate your request, you should define how CustomID look like and behave.

var LoginRequest = new LoginWithCustomIDRequest
{
   // Custom unique identifier for the user, use anything you want.
   CustomId = "ConsoleAuthentication",
   // Automatically create a PlayFab account if one is not currently linked to this ID.
   CreateAccount = true,
   // Request additional Information
   InfoRequestParameters = LoginRequestParams
};

You can also request additional information on login to initialize your game or your player profile.

For more information see : getplayercombinedinforequestparams

var LoginRequestParams = new GetPlayerCombinedInfoRequestParams
{
	// Get Player Profile
	GetPlayerProfile = true,
    // Get Player Currency
	GetUserVirtualCurrency = true
};

Call LoginWithCustomID with PlayFab API

You are ready to send your authentication request and catch the answer. In the Main Function, we add the following

PlayFabClientAPI.LoginWithCustomIDAsync(LoginRequest)
  .ContinueWith(OnLoginCompleted)
  .Wait();

Manage PlayFab answer

We create a new function (OnLoginCompleted) that will print confirmation of the API call using messages written to the console output.

 private static void OnLoginCompleted(Task<PlayFabResult<LoginResult>> loginTask)
 {
   	var apiError = loginTask.Result.Error;
   	var apiResult = loginTask.Result.Result;

   	var PlayerProfile = apiResult.InfoResultPayload.PlayerProfile;
   	var PlayerCurrencies = apiResult.InfoResultPayload.UserVirtualCurrency;

	if (apiError != null)
   	{
       // When Error Occur
       Console.WriteLine("Something went wrong with your API call :( \nHere's some debug information:");

       // Print issue with the PlayFabUtil class
       Console.WriteLine(PlayFabUtil.GenerateErrorReport(apiError));
       Console.WriteLine(apiError.GenerateErrorReport());
     }
     else
     {
       // Display Player information
       Console.WriteLine("Congratulations, you made a successful API call! \n");
       Console.WriteLine("Player information:");

       // Player Session
       Console.WriteLine("PlayFabId:           " + apiResult.PlayFabId);
       Console.WriteLine("SessionTicket:       " + apiResult.SessionTicket);
       // Player Activity
       Console.WriteLine("NewlyCreated:        " + apiResult.NewlyCreated);
       Console.WriteLine("LastLoginTime:       " + apiResult.LastLoginTime);

       // Information get with the LoginRequestParams
       if (PlayerProfile != null)
       { 
         // About GetPlayerProfile
         Console.WriteLine("\nWe get the following information from GetPlayerProfile \n");
         Console.WriteLine("DisplayName:         " + PlayerProfile.DisplayName);
         Console.WriteLine("Created:             " + PlayerProfile.Created);
         Console.WriteLine("ValueToDateInUSD:    " + PlayerProfile.TotalValueToDateInUSD);
       }
       // About GetUserVirtualCurrency
       if (PlayerCurrencies != null)
       {
         Console.WriteLine("\nWe get the following information from GetUserVirtualCurrency \n");
         Console.WriteLine("User Currencies own:       " + PlayerCurrencies.Count);

         foreach (var PlayerCurrency in PlayerCurrencies)
         {
           Console.WriteLine("---");
           Console.WriteLine("Currency:            " + PlayerCurrency.Key);
           Console.WriteLine("Value:               " + PlayerCurrency.Value);
         }
       }                
 	}
 }

You can extend the OnLoginCompleted with information requested with the LoginRequestParams. You want to review the full code source or detect an issue? Please use: YAKAGameStudio (github.com)

Conclusion

If you follow this step-by-step tutorial, you should have the following in result

Share for immortality
Laurent LEFEVRE
Laurent LEFEVRE

I'm a Microsoft employee, working on Technology Strategy for large Enterprise account.
I focus on industry business trends and explain how you can leverage Azure Game Stack & Xbox services.
I assume that I don't know anything. This is a great habit to do a full knowledge refresh and avoiding bias.