sozsoft-platform/api/src/Sozsoft.Platform.HttpApi.Host/Identity/PlatformSessionClaimContext.cs

34 lines
780 B
C#
Raw Normal View History

using System;
using System.Threading;
namespace Sozsoft.Platform.Identity;
public static class PlatformSessionClaimContext
{
private static readonly AsyncLocal<string> CurrentSessionId = new();
public static string SessionId => CurrentSessionId.Value;
public static IDisposable Change(string sessionId)
{
var previous = CurrentSessionId.Value;
CurrentSessionId.Value = sessionId;
return new DisposeAction(() => CurrentSessionId.Value = previous);
}
private sealed class DisposeAction : IDisposable
{
private readonly Action action;
public DisposeAction(Action action)
{
this.action = action;
}
public void Dispose()
{
action();
}
}
}