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

35 lines
697 B
C#
Raw Normal View History

namespace Bunny.Common.Context;
public class BaseContext
{
// 使用Lazy初始化确保线程安全
public static readonly ThreadLocal<long?> UserId = new();
public static readonly ThreadLocal<string?> Token = new();
// 用户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()
{
Token.Value = null;
UserId.Value = null;
}
}