CSharp-Single-EFCore/Bunny.Common/Context/BaseContext.cs

35 lines
740 B
C#

namespace Bunny.Common.Context;
public class BaseContext
{
// 使用Lazy初始化确保线程安全
public static readonly ThreadLocal<long?>? UserId = null;
public static readonly ThreadLocal<string?>? Token = null;
// 用户id相关
public static long? GetUserId()
{
return UserId?.Value;
}
public static void SetUserId(long? userId)
{
UserId!.Value = userId;
}
public static string? GetToken()
{
return Token?.Value;
}
public static void SetToken(string token)
{
Token!.Value = token;
}
public static void RemoveUser()
{
if (Token != null) Token.Value = null;
if (UserId != null) UserId.Value = null;
}
}