using System; using Windows.Security.Credentials; namespace BetterLyrics.WinUI3.Helper { public static class PasswordVaultHelper { /// /// 保存敏感数据到 PasswordVault /// /// 资源标识,比如 "MyApp" /// 键名,比如 "SessionKey" /// 要保存的值 public static void Save(string resource, string key, string value) { try { var vault = new PasswordVault(); vault.Add(new PasswordCredential(resource, key, value)); } catch (Exception) { } } /// /// 从 PasswordVault 获取敏感数据 /// /// 资源标识 /// 键名 /// 存储的值,若不存在则返回 null public static string? Get(string resource, string key) { try { var vault = new PasswordVault(); var credential = vault.Retrieve(resource, key); credential.RetrievePassword(); return credential.Password; } catch (Exception) { return null; } } /// /// 删除指定的敏感数据 /// public static void Delete(string resource, string key) { try { var vault = new PasswordVault(); var credential = vault.Retrieve(resource, key); vault.Remove(credential); } catch (Exception) { } } } }