Authentication Native SDK
Implement player authentication with the Native SDK to sign in anonymously, refresh tokens, and manage player sessions with OpenID providers.
阅读时间5 分钟最后更新于 9 小时前
Prerequisites
To get started with Unity Authentication, you need to do the following:
Get started
To get started with the Native SDK, follow these steps:
- In the Unity Dashboard, select Development > Settings to retrieve your Project ID from the Project settings page.
- Retrieve your environment name from the Environments page.
- In the Unity Dashboard, select Player Authentication > Packages & SDKs, then download the Native SDK for your platform.
- Create a object.
unity::uaslib::AuthenticationClient - Use the and
SetProjectIdmethods on theSetEnvironmentNameobject you created in the previous step to set those values to the ones from the Unity Dashboard.AuthenticationClient - Run , and take the resulting
SignInAnonymouslyandPlayerIdfrom theIdToken.AuthenticationSignInResponse- Alternatively, after the callback has fired successfully, get and
PlayerIdby usingIdTokenandAuthenticationClient::GetToken.AuthenticationClient::GetPlayerId
- Alternatively, after the callback has fired successfully, get
Response structures
AuthenticationSignInCallbackAuthenticationSignInResponseSignInAnonymouslyRefreshTokenThe contains the following information:
AuthenticationSignInResponse- : the ID token that's returned by the
IdTokencall.AuthenticationSignIn - : the player ID that's associated with the ID token.
PlayerId - : an
ExpiresInvalue that represents the number of seconds before the token expires.int
The following is an example of an AuthenticationClient and sign-in callback:
unity::uaslib::AuthenticationClient client; client.SetEnvironmentName(UNITY_ENVIRONMENT_NAME); client.SetProjectId(UNITY_PROJECT_ID); Unity::Authentication::AuthenticationSignInCallback cb; cb = [](const unity::uaslib::AuthenticationSignInResponse &response) { std::cout << "Authentication Sign In Response:\n"; std::cout << "Token: " << response.Token << "\n"; std::cout << "PlayerId: " << response.PlayerId << "\n"; std::cout << "ExpiresIn: " << response.ExpiresIn << "\n"; }; client.SignInAnonymously(cb); //Running ClearSessionToken prior to another SignInAnonymously //will result in a new PlayerId client.ClearSessionToken(); client.SignInAnonymously(cb); client.RefreshToken(cb);
Refresh a token
After sign-in, the SDK can extend the authentication for a specific for a session by refreshing the authentication token. To refresh the token, call , which will re-authenticate the same and return a new token in the callback.
PlayerIdRefreshTokenPlayerIdcb = [&tmpProcessing](const unity::uaslib::AuthenticationSignInResponse &response) { std::cout << "Authentication Sign In Response:\n"; std::cout << "Token: " << response.Token << "\n"; std::cout << "PlayerId: " << response.PlayerId << "\n"; std::cout << "ExpiresIn: " << response.ExpiresIn << "\n"; tmpProcessing = false; }; client.RefreshToken(cb);
Delete a player
An active Authentication session uses the method to delete the currently signed-in player from the Authentication service. After the service confirms the deletion, the clears the cached and , so the player must sign in again.
DeletePlayerAuthenticationClientTokenPlayerIddeleteCb = [](const unity::uaslib::AuthenticationDeletePlayerResponse &response) { std::cout << "Authentication Delete Player Response:\n"; std::cout << (response.IsSuccessful() ? "Completed Successful\n" : "Completed Failure\n"); tmpProcessing = false; };client.DeletePlayer(deleteCb);
OpenID sign-in, linking, and unlinking
Add an OpenID Connect ID Provider to the Unity Dashboard to use OpenID sign-ins. Navigate to Player Authentication > Identity Providers to set the OpenID Connect ID provider.
OpenID Sign-in
You can create new Unity Authentication sessions connected to the after you have set up an OpenID ID Provider on the Unity Dashboard. Provide a token from the service and the IdProvider name, along with an optional to SignInOpenID to create the new session.
IdProviderAuthenticationSignInCallbackAn associated AuthenticationPlayer containing all connected is available through the method.
IdProvidersGetPlayer()auto IdToken = "AnOpenIdToken"; auto IdProvider = "OpenIdProviderName"; cb = [this](const unity::uaslib::AuthenticationSignInResponse &response) { std::cout << "Authentication Sign In Response:\n"; std::cout << "Token: " << response.Token << "\n"; std::cout << "PlayerId: " << response.PlayerId << "\n"; std::cout << "ExpiresIn: " << response.ExpiresIn << "\n"; }; client.SignInOpenID(IdToken, IdProvider, cb);
Linking
Using the method with an from the service, the name, and an additional , you can associate an OpenID connection with an anonymous authentication session.
LinkOpenID()IdTokenIdProviderunity::uaslib::AuthenticationLinkPlayerCallbackYou can also add an additional OpenID connection to an existing OpenID-connected authentication session.
unity::uaslib::AuthenticationLinkPlayerCallback cb; auto IdToken = "AnOpenIdToken"; auto IdProvider = "OpenIdProviderName"; cb = [this](const unity::uaslib::AuthenticationLinkPlayerResponse &response) { std::cout << "Authentication Link Response:\n"; std::cout << "PlayerId: " << response.PlayerId << "\n"; }; client.LinkOpenID(IdToken, IdProvider, cb);
Unlinking
You can remove an OpenID connection from an OpenID connected authentication session by running with the and . The and are accessible from the ExternalId’s vector of an retrieved from either the SignIn or Link responses or using the method.
UnlinkOpenID()idProviderexternalIdidProviderexternalIdAuthenticationPlayerGetPlayer()The following is an example of an AuthenticationClient and sign-in callback:
unity::uaslib::AuthenticationClient client; client.SetEnvironmentName(UNITY_ENVIRONMENT_NAME); client.SetProjectId(UNITY_PROJECT_ID); unity::uaslib::AuthenticationSignInCallback cb; cb = [](const unity::uaslib::AuthenticationSignInResponse &response) { std::cout << "Authentication Sign In Response:\n"; std::cout << "Token: " << response.Token << "\n"; std::cout << "PlayerId: " << response.PlayerId << "\n"; std::cout << "ExpiresIn: " << response.ExpiresIn << "\n"; }; client.SignInAnonymously(cb); //Running ClearSessionToken prior to a another SignInAnonymously //will result in a new PlayerId client.ClearSessionToken(); client.SignInAnonymously(cb); client.RefreshToken(cb);