Xamarin.Auth: Account data not persisted when app is updated

HuseyinUsluHuseyinUslu TRMember

I'm using Xamarin.Auth to authenticate with services like foursquare and so. I've gotten my authentication code working all good, the problem is that the account data is not persisted when I deploy a new version of my app - each time I deploy a test version on the phone, I've to re-authenticate.

I've searched the forums and stackoverflow, but couldn't find any solutions. There only exists a similar un-answered question on stackoverflow; http://stackoverflow.com/questions/16257519/xamarin-accountstore-account-not-persisting-when-app-is-updated

Here's how I store the account;

    /// <summary>
    /// Stores the account.
    /// </summary>
    private void StoreAccount(AuthenticatorCompletedEventArgs eventArgs)
    {
        if (!eventArgs.IsAuthenticated) // make sure we are authenticated.
        {
            Log.Debug(Logging.AppTag, "FourSquareClient can't store account as auth. is cancelled. ");
            return;
        }

        this.IsAuthenticated = true;
        this.Account = eventArgs.Account;

        AccountStore.Create(this.OwnerContext).Save(eventArgs.Account, "Foursquare");
    }

and here is how I check if we have a stored account;

    /// <summary>
    /// Retrieves stored account.
    /// </summary>
    private void RetrieveAccount()
    {
        if (this.IsAuthenticated)
        {
            Log.Debug(Logging.AppTag, "FourSquareClient is already authenticated! ");
            return;
        }


        var accounts = AccountStore.Create(this.OwnerContext).FindAccountsForService("Foursquare");
        var enumerable = accounts as IList<Account> ?? accounts.ToList();

        if (enumerable.Any())
        {
            Log.Info(Logging.AppTag, "Foursquareclient found account data.");
            this.IsAuthenticated = true;
            this.Account = enumerable.First();
        }
        else
        {
            Log.Info(Logging.AppTag, "Foursquareclient no account data found!");
            this.IsAuthenticated = false;
            this.Account = null;
        }
    }

Answers

  • JanettHolstJanettHolst DKUniversity ✭✭

    I know this is an old post, but since I found it, I suspect that others will as well.

    I solved this by saving the entire Account object in the SQLite database.
    Just call the following to get a string which can be saved in the db.

    JsonConvert.SerializeObject(account)

    and

    JsonConvert.DeserializeObject<Account>(content)

    to get the account again when loading the app. If the account is not null, then it can be saved in the AccountStore and login can be skipped because the user is already logged in.

    AccountStore.Create().Save(account, _serviceName);

  • JanettHolstJanettHolst DKUniversity ✭✭

    Also, please consider encrypting the db if you are saving the account object there. For Android, at least, this would otherwise be a very insecure solution.

Sign In or Register to comment.