Skip to main content
Version: 5.7.0

AuthenticateAnonymous

To authenticate anonymously you must have first called InitializeIdentity. You must supply an anonymous ID if you are creating a brand new profile or if you are logging into an already created profile you need to supply the anonymous ID AND the profile ID of the user.

Once you've logged in successfully make sure to save the anonymous and profile ID for future logins.

You can generate a new anonymous ID using the convenience method GenerateAnonymousId.

Error Handling Example

public void FailureCallback(int statusCode, int reasonCode, string statusMessage, object cbObject) {
switch (reasonCode) {
case ReasonCodes.MISSING_IDENTITY_ERROR: { // Anonymous id is invalid

// Clear the profile id, generate a new Anonymous id, and re-authenticate
BrainCloudClient.Get().AuthenticationService.ClearSavedProfileID();
BrainCloudClient.Get().AuthenticationService.AnonymousId =
BrainCloudClient.Get().AuthenticationService.GenerateAnonymousId();
BrainCloudClient.Get().AuthenticationService.AuthenticateAnonymous(true, OnSuccess_Authenticate, OnError_AuthenticateAnonymous);
break;
}
case ReasonCodes.MISSING_PROFILE_ERROR: { // Anonymous id doesn't exist in database

// The account doesn't exist - create it now.
BrainCloudClient.Get().AuthenticationService.AuthenticateAnonymous(true, OnSuccess_Authenticate, OnError_AuthenticateAnonymous);
break;
}
case ReasonCodes.SWITCHING_PROFILES: { // Identity belongs to a different profile

// Clear the profile id, generate a new Anonymous id, and re-authenticate
BrainCloudClient.Get().AuthenticationService.ClearSavedProfileID();
BrainCloudClient.Get().AuthenticationService.AnonymousId =
BrainCloudClient.Get().AuthenticationService.GenerateAnonymousId();
BrainCloudClient.Get().AuthenticationService.AuthenticateAnonymous(true, OnSuccess_Authenticate, OnError_AuthenticateAnonymous);
break;
}
case ReasonCodes.SECURITY_ERROR: { // Identity is invalid
// Generate a new Anonymous id, and re-authenticate
BrainCloudClient.Get().AuthenticationService.AnonymousId =
BrainCloudClient.Get().AuthenticationService.GenerateAnonymousId();
BrainCloudClient.Get().AuthenticationService.AuthenticateAnonymous(true, OnSuccess_Authenticate, OnError_AuthenticateAnonymous);
break;
}
case ReasonCodes.MISSING_REQUIRED_PARAMETER: { // Anonymous id cannot be blank
// Generate an Anonymous id before calling AuthenticateAnonymous
BrainCloudClient.Get().AuthenticationService.AnonymousId =
BrainCloudClient.Get().AuthenticationService.GenerateAnonymousId();
BrainCloudClient.Get().AuthenticationService.AuthenticateAnonymous(true, OnSuccess_Authenticate, OnError_AuthenticateAnonymous);
break;
}
default: { // Uncaught reasonCode
/**
* Log the unexpected reasonCode to your own internal logs,
* to implement needed error handling later
*/
break;
}
}
}
caution

Make sure you've initialized the brainCloud library before authenticating.

tip
ServiceOperation
authenticationV2AUTHENTICATE

Method Parameters

ParameterDescription
forceCreateIf set to true, create a new profile if anonymous ID not found. If set to false and anonymous ID does not exist on the server, return an error.

Usage

http://localhost:3000
bool forceCreate = true;
SuccessCallback successCallback = (response, cbObject) =>
{
Debug.Log(string.Format("Success | {0}", response));
};
FailureCallback failureCallback = (status, code, error, cbObject) =>
{
Debug.Log(string.Format("Failed | {0} {1} {2}", status, code, error));
};

_bc.AuthenticationService.AuthenticateAnonymous(forceCreate, successCallback, failureCallback);
JSON Response
{
"status": 200,
"data": {
"vcPurchased": 0,
"experiencePoints": 100,
"refundCount": 0,
"playerSessionExpiry": 60,
"server_time": 1464621990155,
"experienceLevel": 0,
"currency": {
"credits": {
"purchased": 0,
"balance": 12211,
"consumed": 133,
"awarded": 12344
}
},
"abTestingId": 8,
"statistics": {
"gamesWon": 0
},
"id": "323e861-b749-4ce4-a57a-175232e21b5d",
"createdAt": 1459439058035,
"profileId": "323e861-b749-4ce4-a57a-175232e21b5d",
"newUser": "false",
"xpCapped": false,
"sent_events": [],
"timeZoneOffset": -5,
"playerName": "",
"vcClaimed": 0,
"parentProfileId": null,
"rewards": {
"rewardDetails": {},
"rewards": {},
"currency": {}
},
"countryCode": "ca",
"loginCount": 16,
"emailAddress": "test@email.com",
"previousLogin": 1464621979514,
"incoming_events": [],
"lastLogin": 1464621990118,
"languageCode": "en",
"pictureUrl": null,
"sessionId": "v3grtg3ve0a089pekk8lneuk8k",
"amountSpent": 0
}
}
Common Error Code

Status Codes

CodeNameDescription
40206MISSING_IDENTITY_ERRORBoth an anonymousId and profileId were provided for authentication - but the anonymousId doesn't exist on the server. The profileId may or may not exist. It is possible that the user account was deleted via the Design Portal. The proper recourse is to reset the stored profile id, and re-authenticate. [There is no need to delete the anonymousId since it doesn't exist on the server anyway.]
40207SWITCHING_PROFILESThis means that the anonymousId provided does point to a profile, but not the same one that was saved in the client. This fails the anonymous security check. For any other authentication type, this might indicate that the user wants to switch accounts (thus the name of the error constant). For anonymous, the only response is to reset both the stored anonymousId and profileId, and then re-authenticate.
40208MISSING_PROFILE_ERRORThe anonymousId provided is not associated with an existing profile and forceCreate = false. To create an account, retry with forceCreate = true.
40209SECURITY_ERROROccurs when attempting to authenticate anonymously to an existing user without providing the matching profile ID
40217UNKNOWN_AUTH_ERRORAn unknown error has occurred on authentication
40358MISSING_REQUIRED_PARAMETERThe provided anonymous ID cannot be null