public async Task Invoke(HttpContext httpContext) { bool isNewSessionKey = !httpContext.Request.Headers.TryGetValue("x-sid", out StringValues sessionId); httpContext.Features.Set <ISessionFeature>(new SessionFeature { Session = SessionStore.InitSession(httpContext, sessionId.ToString(), isNewSessionKey) }); await httpContext.Session.LoadAsync(); httpContext.Response.OnStarting(() => { if (string.IsNullOrEmpty(httpContext.Session.Id)) { httpContext.Response.Headers["x-sid"] = "DELETED"; } else { httpContext.Response.Headers["x-sid"] = httpContext.Session.Id; } return(Task.CompletedTask); }); try { await _next(httpContext); } finally { if (httpContext.Session.IsAvailable) { await httpContext.Session.CommitAsync(); } } }