45 lines
1.3 KiB
C#
45 lines
1.3 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
|
|
namespace ZNTechClone.Services
|
|
{
|
|
/// <summary>“记住账号 / 密码”的本地缓存(仅演示用途,明文存储于用户目录)。</summary>
|
|
public static class CredentialStore
|
|
{
|
|
private static readonly string PathFile = Path.Combine(DataRoot.Dir, "remember.json");
|
|
|
|
public static (string user, string pwd)? Load()
|
|
{
|
|
try
|
|
{
|
|
if (!File.Exists(PathFile)) return null;
|
|
var dto = JsonSerializer.Deserialize<Dto>(File.ReadAllText(PathFile));
|
|
return dto == null || string.IsNullOrEmpty(dto.user) ? null : (dto.user, dto.pwd);
|
|
}
|
|
catch { return null; }
|
|
}
|
|
|
|
public static void Save(string user, string pwd)
|
|
{
|
|
try
|
|
{
|
|
Directory.CreateDirectory(Path.GetDirectoryName(PathFile));
|
|
File.WriteAllText(PathFile, JsonSerializer.Serialize(new Dto { user = user, pwd = pwd }));
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
public static void Clear()
|
|
{
|
|
try { if (File.Exists(PathFile)) File.Delete(PathFile); } catch { }
|
|
}
|
|
|
|
private class Dto
|
|
{
|
|
public string user { get; set; }
|
|
public string pwd { get; set; }
|
|
}
|
|
}
|
|
}
|