Beispiel #1
0
        /// <summary>
        /// Authenticate a user in PlayFab using an Email & Password combo
        /// </summary>
        protected virtual async Task AuthenticateEmailPassword()
        {
            //Check if the users has opted to be remembered.
            if (RememberMe && !string.IsNullOrEmpty(RememberMeId))
            {
                //If the user is being remembered, then log them in with a customid that was
                //generated by the RememberMeId property
                var customIdResult = await PlayFabClient.LoginWithCustomIDAsync(new LoginWithCustomIDRequest()
                {
                    TitleId               = PlayFabSettings.staticSettings.TitleId,
                    CustomId              = RememberMeId,
                    CreateAccount         = true,
                    InfoRequestParameters = this.InfoRequestParams
                }).ConfigureAwait(false);

                LoginResult(customIdResult);

                return;
            }
            else if (string.IsNullOrEmpty(Email) || string.IsNullOrEmpty(Password))
            {
                //a good catch: If username & password is empty, then do not continue, and Call back to Authentication UI Display
                OnDisplayAuthentication.Invoke();
                return;
            }
            else
            {
                //We have not opted for remember me in a previous session, so now we have to login the user with email & password.
                var emailResult = await PlayFabClient.LoginWithEmailAddressAsync(new LoginWithEmailAddressRequest()
                {
                    TitleId  = PlayFabSettings.staticSettings.TitleId,
                    Email    = this.Email,
                    Password = this.Password,
                    InfoRequestParameters = this.InfoRequestParams
                }).ConfigureAwait(false);

                //Note: At this point, they already have an account with PlayFab using a Username (email) & Password
                //If RememberMe is checked, then generate a new Guid for Login with CustomId.
                if (RememberMe)
                {
                    RememberMeId = Guid.NewGuid().ToString();
                    AuthType     = AuthType.EmailAndPassword;

                    //Fire and forget, but link a custom ID to this PlayFab Account.
                    await PlayFabClient.LinkCustomIDAsync(new LinkCustomIDRequest()
                    {
                        CustomId  = RememberMeId,
                        ForceLink = ForceLink
                    }).ConfigureAwait(false);
                }

                LoginResult(emailResult);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Register a user with an Email & Password
        /// Note: We are not using the RegisterPlayFab API
        /// </summary>
        protected virtual async Task AddAccountAndPassword()
        {
            //Any time we attempt to register a player, first silently authenticate the player.
            //This will retain the players True Origination (Android, iOS, Desktop)
            await SilentlyAuthenticate(CrossDeviceInfo.Current.Platform, async (result) =>
            {
                if (result == null)
                {
                    //something went wrong with Silent Authentication, Check the debug console.
                    Logout();
                    OnPlayFabError?.Invoke(new PlayFabError()
                    {
                        Error        = PlayFabErrorCode.UnknownError,
                        ErrorMessage = "Silent Authentication by Device failed"
                    });
                }

                //Note: If silent auth is success, which is should always be and the following
                //below code fails because of some error returned by the server ( like invalid email or bad password )
                //this is okay, because the next attempt will still use the same silent account that was already created.

                //Now add our username & password.
                var addUsernameResult = await PlayFabClient.AddUsernamePasswordAsync(new AddUsernamePasswordRequest()
                {
                    Username = !string.IsNullOrEmpty(this.Username) ? Username : result.PlayFabId,                     //Because it is required & Unique and not supplied by User.
                    Email    = this.Email,
                    Password = this.Password,
                }).ConfigureAwait(false);

                if (null != addUsernameResult.Error)
                {
                    //report error back to subscriber
                    Logout();
                    OnPlayFabError?.Invoke(addUsernameResult.Error);
                }
                else
                {
                    //Store identity and session
                    PlayFabId     = result.PlayFabId;
                    SessionTicket = result.SessionTicket;

                    //If they opted to be remembered on next login.
                    if (RememberMe)
                    {
                        //Generate a new Guid
                        RememberMeId = Guid.NewGuid().ToString();

                        //Fire and forget, but link the custom ID to this PlayFab Account.
                        await PlayFabClient.LinkCustomIDAsync(new LinkCustomIDRequest()
                        {
                            CustomId  = RememberMeId,
                            ForceLink = ForceLink
                        }).ConfigureAwait(false);
                    }

                    //Override the auth type to ensure next login is using this auth type.
                    AuthType = AuthType.EmailAndPassword;

                    //Report login result back to subscriber.
                    OnLoginSuccess?.Invoke(result);
                }
            }).ConfigureAwait(false);
        }