【问题标题】:Xamarin Auth account storeXamarin 身份验证帐户存储
【发布时间】:2018-10-11 20:25:05
【问题描述】:

我正在尝试使用我的应用程序实现 Xamairn Auth。我已经从https://www.nuget.org/packages/Xamarin.Auth 安装了 nuget 包。

按照他们的示例,我在共享项目中有以下代码。

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

在 android 上运行时,它会保存用户名和密码,但我在控制台中收到以下消息:

"此版本不安全,因为默认密码。 请使用提供 AccountStore 密码的版本。 AccountStore.Create(Context, string) 或 AccountStore.Create(string);"

我尝试将参数传递给 AccountStore.Create() 方法,但它似乎没有接受。像这样的:

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

我是否需要编写特定于 android 的代码来扩展 create 方法。

【问题讨论】:

    标签: xamarin oauth xamarin.forms xamarin.auth


    【解决方案1】:

    我理解您为什么删除非答案,我认为这会显示出对这个问题的兴趣。我想我应该赞成这个问题。无论如何,这是我找到的答案。

    你不能使用安卓的 PCL 版本。它没有添加密码的选项。我使用了android特定版本。将使用依赖服务调用它。

    这是一个例子:

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

    【讨论】:

    • 感谢我稍加修改就能让它工作。
    • 顺便说一句,我没有删除任何非答案。
    【解决方案2】:

    我能够通过在 android 中实现一个 getAccountStore 方法来使其工作,该方法具有添加密码的选项,然后使用 DependencyService 调用它。

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

    然后在你的 pcl 项目中这样调用它:

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

    【讨论】:

      猜你喜欢
      • 2012-08-24
      • 2021-12-04
      • 2019-12-17
      • 2021-07-30
      • 1970-01-01
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多