簡體   English   中英

如何使用Xamarin.auth切換登錄的用戶

[英]How to switch logged user using Xamarin.auth

我正在使用Xamarin.Auth來保存憑據,並在Xamarin Forms應用程序中登錄用戶。 現在,我需要實現一個“切換登錄用戶”,但是我不知道如何正確執行此操作。

在互聯網上什么都沒有。 因此,如果有人可以解釋或說明如何做到這一點。

要檢查是否已保存帳戶:

IEnumerable<Account> accounts = AccountStore.Create().FindAccountsForService(InstaConstants.AppName);

但始終只使用一個帳戶,而我正在測試而不刪除舊憑據。

看看這個例子。 它顯示了用戶登錄后如何調用Completed事件,如何檢查以確保他們已登錄,然后保存將access_token存儲在eventArgs.Account.Properties["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 account

如果您沒有限制只使用1個商店,則可以給商店命名,例如“ MySupaApp” + iteration.ToString();。 因此,您將迭代所有已保存的用戶。

另一種巧妙的方法是使用json將用戶列表保存到1個帳戶中。

//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


            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM