Steam
Minimum SDK version: 2.0.0
This article guides you through the following scenarios in setting up authentication for player's 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
Create your app and note down the app ID following the Steamworks documentation.
Create the Publisher Web API key following the Authentication using Web API Keys documentation.
Install the SteamWorks.Net SDK following instructions to install the SDK specifically for Unity.
- Copy the SteamWorks.Net SDK into your project’s Assets/ folder.
- Open the steam_appid.txt in the root of your Unity project and replace 480 with your own App ID.
- Add the Steam Manager game component to a game object in the scene. It will initialize the steam library.
- Before you test, make sure Steam is installed and logged in. The Steam user has the game you are developing in the library.
Configure your ID provider to be Steam for Unity Authentication:
- In the Unity Editor menu, go to Edit > Project Settings…, then select Services > Authentication from the navigation menu.
- Set ID Providers to Steam, then select Add.
- Enter the app ID in the App ID text field.
- Enter the Publisher Web API Key in the Key text field, then select Save.
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 is 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 an example of a good usage 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, so make sure to request the ticket using the same string following the same requirements.
Callback<GetTicketForWebApiResponse_t> m_AuthTicketForWebApiResponseCallback;
HAuthTicket m_AuthTicket;
string m_SessionTicket;
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);
m_AuthTicket = SteamUser.GetAuthTicketForWebApi("unityauthenticationservice");
}
void OnAuthCallback(GetTicketForWebApiResponse_t callback)
{
Token = BitConverter.ToString(callback.m_rgubTicket).Replace("-", string.Empty);
m_AuthTicketResponseCallback.Dispose();
m_AuthTicketResponseCallback = null;
Debug.Log("Steam Login success. Session Ticket: " + m_SessionTicket);
// Call Unity Authentication SDK to sign in or link with Steam.
}
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
You can 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, then SignInWithSteamAsync will create a new player. If a Unity Authentication player in your project is associated with the credentials, then SignInWithSteamAsync will sign into that player's account. This function doesn't take into consideration the cached player, and SignInWithSteamAsync will replace 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 you’ve set up anonymous authentication, if the player wants to upgrade from being anonymous to creating 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, then you have the option of linking the cached player to the Steam Account.
- Sign into the cached player's account using SignInAnonymouslyAsync.
- Link the cached player's account to the Steam account with LinkWithSteamAsync.
For more information about cached players, read the Sign In a Cached User section.
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);
}
}
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)
.