33 lines
780 B
C#
33 lines
780 B
C#
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();
|
|
}
|
|
}
|
|
}
|