pieperu
pieperu

Reputation: 2762

Storing Access Token and Refresh Token on Xamarin

I have a WebApi 2 using ASP.Net Identity. I want to consume this in a Xamarin application, I'm struggling to find a straight answer to this question:

How can i store the Access Token and Refresh token on the device using Xamarin, similar to Cookies or Session state in ASP.NET?

I'm able to make the call to get these tokens, but i'm new to Xamarin and can't work out how to save them on the device so the user doesn't need to constantly login.

The targets devices are Windows, iOS and Android

Upvotes: 3

Views: 2396

Answers (2)

Mostafa Esmaeili
Mostafa Esmaeili

Reputation: 178

XLabs.Platform has ISecureStorage interface and implementations for iOS/Droid/WP8 with native encrypted storages.
Also You could take a look at the Akavache SecureBlobCache.
Every time Access Token Expired, use Refresh Token TO Gain new Access TOken(Maybe new Refresh TOken)Then replace them ...

Upvotes: 1

dush88c
dush88c

Reputation: 2106

We can use Xamarin.Auth to store data in the account store like below.

Storing Account Information

public void SaveCredentials (string userName, string password)
{
  if (!string.IsNullOrWhiteSpace (userName) && !string.IsNullOrWhiteSpace (password)) {
    Account account = new Account {
      Username = userName
    };
    account.Properties.Add ("Password", password);
    AccountStore.Create ().Save (account, App.AppName);
  }
}

Retrieving Account Information

public string UserName {
  get {
    var account = AccountStore.Create ().FindAccountsForService (App.AppName).FirstOrDefault ();
    return (account != null) ? account.Username : null;
  }
}

public string Password {
  get {
    var account = AccountStore.Create ().FindAccountsForService (App.AppName).FirstOrDefault ();
    return (account != null) ? account.Properties ["Password"] : null;
  }
}

Upvotes: 0

Related Questions