Files
Store.Management/Source/Store.Management.Api/Authentication/TokenSession.cs
T
2026-10-27 14:35:03 +08:00

17 lines
709 B
C#

namespace Store.Management.Api.Authentication;
/// <summary>会话仅保存在内存中,不将密码或 Token 写入配置与日志。</summary>
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);
}
}