我使用Xamarin.Auth保存凭据,并将用户登录到Xamarin窗体应用程序中。现在,我需要实现一个“切换日志用户”,但我没有弄清楚如何正确地做到这一点。
互联网上什么都没有。所以,如果有人能解释或者展示这是怎么做到的。
若要检查帐户是否已保存:
IEnumerable<Account> accounts = AccountStore.Create().FindAccountsForService(InstaConstants.AppName);但总是只有一个帐户,我正在测试而不删除旧凭据。
发布于 2017-10-31 17:37:44
如果您没有限制只使用一个商店,您可以给出像“MySupaApp”+iteration.ToString()这样的商店名称;所以您将迭代您保存的所有用户。
另一种简洁的方法是使用json将用户列表保存到一个帐户中。
//im using Constants.StoreName - you know what it is..
//
//save users
//
List<MyUsers> MyList; // <==users here initially
var jsonUsers = await Task.Run(() => JsonConvert.SerializeObject(MyList));
Account account = new Account();
account.Username = "AllMyUsers";
account.Properties.Add("users", jsonUsers);
//cleanup previous
var accounts = store.FindAccountsForService(Constants.StoreName).ToList();
accounts.ForEach(acc => store.Delete(acc, Constants.StoreName));
//save finally
await store.SaveAsync(account, Constants.StoreName);
//
//read users
//
Account account = store.FindAccountsForService(Constants.StoreName).FirstOrDefault();
if (account == null)
{
//create new empty list of users
//todo
return false;
}
try
{
List<MyUsers> MyList = JsonConvert.DeserializeObject<List<MyUsers>>(account.Properties["users"]);
//todo check stuff if list is valid
return true;
}
catch
{
//todo
//create new empty list
//something went wrong
}发布于 2017-10-31 13:17:20
查看这示例。它展示了用户登录后如何调用Completed事件,以确保它们已登录,然后保存存储在eventArgs.Account.Properties["access_token"]中的access_token。
auth.Completed += (sender, eventArgs) => {
if (eventArgs.IsAuthenticated) {
App.Instance.SuccessfulLoginAction.Invoke();
// Use eventArgs.Account to do wonderful things
App.Instance.SaveToken(eventArgs.Account.Properties["access_token"]);
} else {
// The user cancelled
}
};*编辑:为了在AccountStore中保存多个帐户,只需提供一个不同的provider值:
//FROM
await AccountStore.Create().SaveAsync(eventArgs.Account, "instagram"); //Saving a single general Instagram account
//TO
string someUniqueIdentifier = /* the user's User Id, an incremented number, some other identifier */
await AccountStore.Create().SaveAsync(eventArgs.Account, "instagram" + someUniqueIdentifier); //Ability to save multiple Instagram accounts, someUniqueIdentifier must change for each new accounthttps://stackoverflow.com/questions/47020849
复制相似问题