如何将Google凭证存储到SQLite?GenerateStoredKey参数及DataStore使用疑问
Google凭证存储到SQLite的问题求助
我之前使用GoogleWebAuthorizationBroker将用户凭证保存至本地文件,为提升应用安全性,计划将凭证存储到SQLite数据库。已参考相关内容实现了Entity Framework相关类,但不清楚如何正确使用这些类完成数据库存储,尤其是GenerateStoredKey方法的参数应传入什么值。
当前调用代码
return GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.FromStream(stream).Secrets, _scopes, "user", CancellationToken.None, GoogleCredentialsDataStore.GenerateStoredKey("What string value goes here") );
数据接口实现类
public class GoogleCredentialsDataStore : SqLiteDbContext, IDataStore { /// <summary> /// Database context to access database /// </summary> private readonly SqLiteDbContext _context; public GoogleCredentialsDataStore(SqLiteDbContext context) { _context = context; } /// <summary> /// Stores the given value for the given key. It creates a new row in the database with the user id of /// (primary key <see cref="GenerateStoredKey"/>) in <see cref="GoogleUserCredentials"/>. /// </summary> /// <typeparam name="T">The type to store in the data store.</typeparam> /// <param name="key">The key.</param> /// <param name="value">The value to store in the data store.</param> Task IDataStore.StoreAsync<T>(string key, T value) { if (string.IsNullOrEmpty(key)) { throw new ArgumentNullException("key Must have a value"); } var serialized = NewtonsoftJsonSerializer.Instance.Serialize(value); _context.GoogleCredentials.Add(new() { Key = GenerateStoredKey(key), Credentials = serialized }); _context.SaveChanges(); return Task.Delay(0); } /// <summary> /// Deletes the given key. It deletes the <see cref="GenerateStoredKey"/> row in /// <see cref="GoogleCredentials"/>. /// </summary> /// <param name="key">The key to delete from the data store.</param> Task IDataStore.DeleteAsync<T>(string key) { if (string.IsNullOrEmpty(key)) { throw new ArgumentException("Key MUST have a value"); } try { var hold = _context.GoogleCredentials.Where(a => a.Key == key).FirstOrDefault(); _context.GoogleCredentials.Remove(hold); _context.SaveChangesAsync(); } catch (Exception) { throw new Exception("Failed to delete credentials"); } return Task.Delay(0); } /// <summary> /// Returns the stored value for the given key or <c>null</c> if the matching row (<see cref="GenerateStoredKey"/> /// in <see cref="GoogleCredentials"/> doesn't exist. /// </summary> /// <typeparam name="T">The type to retrieve.</typeparam> /// <param name="key">The key to retrieve from the data store.</param> /// <returns>The stored object.</returns> Task<T> IDataStore.GetAsync<T>(string key) { if (string.IsNullOrEmpty(key)) { throw new ArgumentException("Key MUST have a value"); } TaskCompletionSource<T> tcs = new TaskCompletionSource<T>(); var user = GetUserByKey(GenerateStoredKey(key)); if (user != null) { try { tcs.SetResult(NewtonsoftJsonSerializer.Instance.Deserialize<T>(user.Credentials)); } catch (Exception ex) { tcs.SetException(ex); } } else { tcs.SetResult(default(T)); } return tcs.Task; } /// <summary> /// Clears all values in the data store. This method deletes all files in <see cref="GoogleCredentials"/>. /// </summary> Task IDataStore.ClearAsync() { try { foreach (var item in _context.GoogleCredentials) { _context.GoogleCredentials.Remove(item); } } catch (Exception) { throw new Exception("Failed to clear credentials"); } return Task.Delay(0); } /// <summary> /// Checks if the user exists <see cref="GenerateStoredKey"/>. /// </summary> private GoogleCredentials GetUserByKey(string key) { try { var user = _context.GoogleCredentials.Where(a => a.Key == key).FirstOrDefault(); if (user != null) return user; return null; } catch (Exception) { return null; } } /// <summary> /// Save the credentials. If the user <see cref="GenerateStoredKey"/> does not exists we insert it other wise we will do an update. /// </summary> /// <param name="key"></param> /// <param name="serialized"></param> private void save(string key, string serialized) { try { var user = _context.GoogleCredentials.Where(a => a.Key == key).FirstOrDefault(); if (user == null) { var hold = new GoogleCredentials { Key = key, Credentials = serialized }; _context.GoogleCredentials.Add(hold); } else { var aUser = _context.GoogleCredentials.Where(a => a.Key == key).FirstOrDefault(); aUser.Credentials = serialized; } _context.SaveChanges(); } catch (Exception) { throw; } } /// <summary>Creates a unique stored key based on the key and the current project name.</summary> /// <param name="key">The object key.</param> public static string GenerateStoredKey(string key) { return string.Format("{0}-{1}", Assembly.GetCallingAssembly().GetName().Name, key); } }
Google凭证模型类
public class GoogleCredentials { [Key] public int Id { get; set; } [Required, StringLength(500)] public string Key { get; set; } [Required] public string Credentials { get; set; } }
我是新手,目前认为方向正确,但希望得到资深开发者的指导,帮我走上正确路径。
内容的提问来源于stack exchange,提问作者DRW
相关产品推荐
相关产品推荐

