public override async Task SaveAsync(Account account, string serviceId) { byte[] data = Encoding.UTF8.GetBytes(account.Serialize()); byte[] prot = (await DataProtectionExtensions.ProtectAsync(data).ConfigureAwait(false)).ToArray(); var path = GetAccountPath(account, serviceId); var localFolder = ApplicationData.Current.LocalFolder; var file = await localFolder.CreateFileAsync(path, CreationCollisionOption.OpenIfExists).AsTask().ConfigureAwait(false); using (var stream = await file.OpenStreamForWriteAsync().ConfigureAwait(false)) using (var writer = new BinaryWriter(stream)) { writer.Write(prot.Length); writer.Write(prot); } }
public override async Task <List <Account> > FindAccountsForServiceAsync(string serviceId) { var localFolder = ApplicationData.Current.LocalFolder; var files = await localFolder.GetFilesAsync().AsTask().ConfigureAwait(false); var accounts = new List <Account>(); foreach (var file in files.Where(x => x.Name.StartsWith("xamarin.auth.") && x.Name.EndsWith("." + serviceId)) .ToList()) { using (var stream = await file.OpenStreamForReadAsync().ConfigureAwait(false)) using (var reader = new BinaryReader(stream)) { var length = reader.ReadInt32(); var data = reader.ReadBytes(length); var unprot = (await DataProtectionExtensions.UnprotectAsync(data).ConfigureAwait(false)).ToArray(); accounts.Add(Account.Deserialize(Encoding.UTF8.GetString(unprot, 0, unprot.Length))); } } return(accounts); }