Steam

Minimum SDK version: 2.0.0

This article guides you through the following scenarios in setting up authentication for players in your game with a Steam account:

  • Set up a Steam account sign-in.
  • Sign in a returning user or create new user.
  • Updating a user from an anonymous sign-in to a platform sign-in via a Steam account.

To provide a steam sign-in option for the players in your game, create an app in the SteamWorks Partner Portal and install the Steamworks.Net SDK to sign in the user and get the session ticket.

Set up a Steam account sign-in

  1. Create your app and note down the app ID following the Steamworks documentation.

  2. Create the Publisher Web API key following the Authentication using Web API Keys documentation.

  3. Install the SteamWorks.Net SDK following instructions to install the SDK specifically for Unity.

    1. Copy the SteamWorks.Net SDK into your project’s Assets/ folder.
    2. Open the steam_appid.txt in the root of your Unity project and replace 480 with your own App ID.
    3. Add the Steam Manager game component to a game object in the scene. It will initialize the steam library.
    4. Before you test, make sure Steam is installed and logged in. The Steam user has the game you are developing in the library.
  4. Configure your ID provider to be Steam for Unity Authentication:

    1. In the Unity Editor menu, go to Edit > Project Settings…, then select Services > Authentication from the navigation menu.
    2. Set ID Providers to Steam, then select Add.
    3. Enter the app ID in the App ID text field.
    4. Enter the Publisher Web API Key in the Key text field, then select Save.

    (Optional) Add Additional App IDs for Demo, PlayTest, Alpha builds. Still in the Authentication Settings.

    1. Select the arrow next to Additional App ID.
    2. Select Add App ID at the bottom of the expanded view.
    3. Enter the App ID and a Description.
    4. Select Save.
  5. Implement the Steam login using the sample code below. Refer to the documentation of GetAuthTicketForWebApi. Unity Authentication SDK accepts only Steam session tickets. Encrypted Application Tickets are not accepted.

Important: It's crucial to register to the OnAuthCallback provided by Steam SDK. It provides a proper auth ticket validation. More details can be found here. The code below is a good example of Steam authentication. Important: Unity Player Authentication only accepts a string containing alphanumeric characters with a length between 5 and 30 as the identity field; make sure to request the ticket using the same string following the same requirements.

Callback<GetTicketForWebApiResponse_t> m_AuthTicketForWebApiResponseCallback;
string m_SessionTicket;
string identity = "unityauthenticationservice"

void SignInWithSteam()
{
    // It's not necessary to add event handlers if they are 
    // already hooked up.
    // Callback.Create return value must be assigned to a 
    // member variable to prevent the GC from cleaning it up.
    // Create the callback to receive events when the session ticket
    // is ready to use in the web API.
    // See GetAuthSessionTicket document for details.
    m_AuthTicketForWebApiResponseCallback = Callback<GetTicketForWebApiResponse_t>.Create(OnAuthCallback);

    SteamUser.GetAuthTicketForWebApi(identity);
}

void OnAuthCallback(GetTicketForWebApiResponse_t callback)
{
    m_SessionTicket = BitConverter.ToString(callback.m_rgubTicket).Replace("-", string.Empty);
    m_AuthTicketForWebApiResponseCallback.Dispose();
    m_AuthTicketForWebApiResponseCallback = null;
    Debug.Log("Steam Login success. Session Ticket: " + m_SessionTicket);
    // Call Unity Authentication SDK to sign in or link with Steam, displayed in the following examples, using the same identity string and the m_SessionTicket.
}

Note: The code example below assumes you already have the player's Steam session ticket using the GetAuthTicketForWebApi method and that you have passed the same identity parameter for issuing the ticket.

Sign in a returning player or create new player

Use the SignInWithSteamAsync method to either:

  • Create a new Unity Authentication player with the Steam credentials.
  • Sign in an existing player using the Steam credentials.

If no Unity Authentication player in your project is associated with the credentials, SignInWithSteamAsync creates a new player. If a Unity Authentication player in your project is associated with the credentials, SignInWithSteamAsync signs into that player's account. This function doesn't consider the cached player, and SignInWithSteamAsync replaces the cached player.

async Task SignInWithSteamAsync(string ticket, string identity)
{
    try
    {
        await AuthenticationService.Instance.SignInWithSteamAsync(ticket, identity);
        Debug.Log("SignIn is successful.");
    }
    catch (AuthenticationException ex)
    {
        // Compare error code to AuthenticationErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
    catch (RequestFailedException ex)
    {
        // Compare error code to CommonErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
}

Update a player from anonymous to a Steam account

After anonymous authentication set up, if the player wants to upgrade from being anonymous to create a Steam account and sign in using a Steam account, the game should prompt the player to trigger the Steam sign-in and get the session ticket from Steam. Then, call the LinkWithSteamAsync API to link the player to the Steam session ticket.

If a cached player exists on the SDK, you can link the cached player to the Steam Account.

  1. Sign into the cached player's account using SignInAnonymouslyAsync.
  2. Link the cached player's account to the Steam account with LinkWithSteamAsync.

For more information about cached players, refer to Sign In a Cached User.

async Task LinkWithSteamAsync(string ticket, string identity)
{
    try
    {
        await AuthenticationService.Instance.LinkWithSteamAsync(ticket, identity);
        Debug.Log("Link is successful.");
    }
    catch (AuthenticationException ex) when (ex.ErrorCode == AuthenticationErrorCodes.AccountAlreadyLinked)
    {
        // Prompt the player with an error message.
        Debug.LogError("This user is already linked with another account. Log in instead.");
    }
    catch (Exception ex)
    {
        Debug.LogError("Link failed.");
        Debug.LogException(ex);
    }
}

When the player triggers the Steam login by signing in or by creating a new player profile, and you have received the Steam access token using the same identity parameter, call the following API to authenticate the player.

async Task SignInWithSteamAsync(string ticket, string identity)
{
    try
    {
        await AuthenticationService.Instance.SignInWithSteamAsync(ticket, identity);
    }
    catch (AuthenticationException ex)
    {
        // Compare error code to AuthenticationErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
    catch (RequestFailedException ex)
    {
        // Compare error code to CommonErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
}

Sign in a returning player or create new player for Additional App IDs

Use the same SignInWithSteamAsync method to sign a player in to an Additional App ID, for example, Demo, PlayTest, Alpha.

Note: The code example below assumes you already have the player's Steam session ticket using the GetAuthTicketForWebApi method and that you have passed the same identity parameter for issuing the ticket.

Important: The player has the same Unity Authentication Player ID for all App IDs under the same project, so use your best judgement to set up different environments to keep the player data (for example, Economy, Cloud Save) scoped to each App ID and avoid mixing production data with non-prodution data.

async Task SignInWithSteamAsync(string ticket, string identity, string appId)
{
    try
    {
        await AuthenticationService.Instance.SignInWithSteamAsync(ticket, identity, appId);
        Debug.Log("SignIn is successful.");
    }
    catch (AuthenticationException ex)
    {
        // Compare error code to AuthenticationErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
    catch (RequestFailedException ex)
    {
        // Compare error code to CommonErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
}

Implement a Steam sign-in authentication

You can use a Steam session ticket to log in or link using a Steam account. Call the following API to sign-in the player: SignInWithSteamAsync(string ticket, string identity).

Unlink Steam account#
Use the `UnlinkSteamAsync` API so your players can unlink their Steam account. Once unlinked, if their account isn’t linked to any additional identity, it transitions to an anonymous account.

async Task UnlinkSteamAsync(string idToken)
{
   try
   {
       await AuthenticationService.Instance.UnlinkSteamAsync(idToken);
       Debug.Log("Unlink is successful.");
   }
   catch (AuthenticationException ex)
   {
       // Compare error code to AuthenticationErrorCodes
       // Notify the player with the proper error message
       Debug.LogException(ex);
   }
   catch (RequestFailedException ex)
   {
       // Compare error code to CommonErrorCodes
       // Notify the player with the proper error message
       Debug.LogException(ex);
   }
}