ClueSelecter
ClueSelecter

Reputation: 151

Xamarin Auth account store

I'm trying to implement Xamairn Auth with my app. I've installed the nuget package from https://www.nuget.org/packages/Xamarin.Auth.

Following their example I have the following code in the shared project.

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);
  }
}

When run on android, it saves the username and password but I'm getting the following message in the console:

"This version is insecure, because of default password. Please use version with supplied password for AccountStore. AccountStore.Create(Contex, string) or AccountStore.Create(string);"

I tried passing a parameter to the AccountStore.Create() method but it doesn't seem to take one. Something like this:

#if ANDROID
   _accountStore = AccountStore.Create(Application.Context);
#else
   _accountStore = AccountStore.Create();
#endif

Do I need to write android specific code to extend the create method.

Upvotes: 1

Views: 2316

Answers (2)

ClueSelecter
ClueSelecter

Reputation: 151

I was able to get it to work by implementing a getAccountStore method in android which has an option to add a password, then use DependencyService to call it.

public AccountStore GetAccountStore()
{
    try
    {
        var acctStore = AccountStore.Create(Application.Context, "somePassword");
        return acctStore;
    }
    catch (Java.IO.IOException ex)
    {
        throw ex;
    }
}

Then in your pcl project call it as such:

 if (Device.RuntimePlatform == Device.Android)
   _accountStore = DependencyService.Get<IAccountStoreHelper>().GetAccountStore();
else
   _accountStore = AccountStore.Create();

Upvotes: 0

me me
me me

Reputation: 11

I understand why you deleted the non-answer, I thought that would show interest in the question. I guess I should have upvoted the question instead. Anyways, here's the answer I found.

You can't use the PCL version for android. It doesn't have an option to add a password. I used the android specific version. Will call it using dependency service.

Here's an example:

    Account account = null;

    try
    {
        //account = AccountStore.Create(Application.ApplicationContext, "System.Char[]").FindAccountsForService("My APP").FirstOrDefault();             
        var aStore = AccountStore.Create(Application.ApplicationContext, "myownpassword");

        // save test
        account = aStore.FindAccountsForService(Constants.AppName).FirstOrDefault();
        if (account == null)
            account = new Account();
        account.Username = "bobbafett";
        account.Properties["pswd"] = "haha";
        aStore.Save(account, Constants.AppName);

        // delete test, doesn't seem to work, account is still found
        var accts = aStore.FindAccountsForService(Constants.AppName);
        int howMany = accts.ToList().Count;
        foreach (var acct in accts)
        {
            aStore.Delete(acct, Constants.AppName);
        }
        account = aStore.FindAccountsForService(Constants.AppName).FirstOrDefault();
    }
    catch (Java.IO.IOException ex)
    {
        // This part is not invoked anymore once I use the suggested password.
        int i1 = 123;
    }

Upvotes: 1

Related Questions