namespace Store.Management.Api.Authentication; /// 会话仅保存在内存中,不将密码或 Token 写入配置与日志。 public sealed class TokenSession { private string? _token; public bool IsAuthenticated => Volatile.Read(ref _token) is not null; internal string? Token => Volatile.Read(ref _token); internal void Set(string token) => Interlocked.Exchange(ref _token, token); public void Clear() => Interlocked.Exchange(ref _token, null); // 旧请求的 401 不应清除重新登录后建立的新会话。 internal void ClearIfCurrent(string? token) { if (token is not null) Interlocked.CompareExchange(ref _token, null, token); } }