Asp Core custom session

public class CustomSession : ISession { private readonly IDistributedCache cache; private readonly string sessionKey; private readonly string sessionId; private Dictionary<string, byte[]> data; public bool IsAvailable { get; set; }

public string Id => _sessionId;

public CustomSession(IDistributedCache cache, string sessionKey, string sessionId = null) { cache = cache; sessionKey = sessionKey; sessionId = sessionId ?? Guid.NewGuid().ToString(); data = new Dictionary<string, byte[]>(); } //public CustomSession( // IDistributedCache cache, // string sessionKey, // TimeSpan idleTimeout, // TimeSpan ioTimeout, // Func tryEstablishSession, // ILoggerFactory loggerFactory, // bool isNewSessionKey) //{ // if (cache == null) // { // throw new ArgumentNullException(nameof(cache)); // }

// if (string.IsNullOrEmpty(sessionKey)) // { // throw new ArgumentException(); // }

// if (tryEstablishSession == null) // { // throw new ArgumentNullException(nameof(tryEstablishSession)); // }

// //if (loggerFactory == null) // //{ // // throw new ArgumentNullException(nameof(loggerFactory)); // //}

// cache = cache; // sessionKey = sessionKey; // //_idleTimeout = idleTimeout; // //_ioTimeout = ioTimeout; // //_tryEstablishSession = tryEstablishSession; // //// When using a NoOpSessionStore, using a dictionary as a backing store results in problematic API choices particularly with nullability. // //// We instead use a more limited contract - IDistributedSessionStore as the backing store that plays better. // //_store = new DefaultDistributedSessionStore(); // //_logger = loggerFactory.CreateLogger(); // //_isNewSessionKey = isNewSessionKey; //}

public async Task LoadAsync(CancellationToken cancellationToken = default) { var data = await cache.GetAsync($"{sessionKey}-{_sessionId}", cancellationToken); if (data != null) { Deserialize(data); } }

public async Task CommitAsync(CancellationToken cancellationToken = default) { var options = new DistributedCacheEntryOptions() .SetSlidingExpiration(TimeSpan.FromMinutes(30));

await cache.SetAsync($"{sessionKey}-{_sessionId}", Serialize(), options, cancellationToken); }

public void Clear() { _data.Clear(); }

public void Remove(string key) { _data.Remove(key); }

public bool TryGetValue(string key, out byte[] value) { return _data.TryGetValue(key, out value); }

public void Set(string key, byte[] value) { _data[key] = value; }

//public bool Remove(string key) //{ // return _data.Remove(key); //}

public IEnumerable Keys => _data.Keys;

private void Deserialize(byte[] data) { _data = JsonConvert.DeserializeObject<Dictionary<string, byte[]>>(Encoding.UTF8.GetString(data)); }

private byte[] Serialize() { return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(_data)); } }

public partial class DistributedSessionStore : ISessionStore { private readonly IDistributedCache cache; private readonly ILoggerFactory loggerFactory;

///

/// Initializes a new instance of . ///
used to store the session data. /// The . public DistributedSessionStore(IDistributedCache cache)//, ILoggerFactory loggerFactory) { if (cache == null) { throw new ArgumentNullException(nameof(cache)); }

//if (loggerFactory == null) //{ // throw new ArgumentNullException(nameof(loggerFactory)); //}

cache = cache; //loggerFactory = loggerFactory; }

/// public ISession Create(string sessionKey, TimeSpan idleTimeout, TimeSpan ioTimeout, Func tryEstablishSession, bool isNewSessionKey) { if (string.IsNullOrEmpty(sessionKey)) { throw new ArgumentException(); }

if (tryEstablishSession == null) { throw new ArgumentNullException(nameof(tryEstablishSession)); }

return new CustomSession(_cache, sessionKey);//, idleTimeout, ioTimeout, tryEstablishSession, _loggerFactory, isNewSessionKey); } }