问题
I followed this guide to set up Sql Server as my store for Session data.
I noticed the Sessions table has an ID column. I was hoping I could use this ID column to determine the current session (row in the Sessions table) being used. However, I don't know how this ID is generated or how ASP.NET Core behind the scenes matches this ID to the Session.
I tried using HttpContext.Session.Id
, but it is different from the ID in the database.
So, how do I determine which row is being used for the Session?
回答1:
It's randomly generated. You can see this in the middleware source
if (string.IsNullOrWhiteSpace(sessionKey) || sessionKey.Length != SessionKeyLength)
{
// No valid cookie, new session.
var guidBytes = new byte[16];
CryptoRandom.GetBytes(guidBytes);
sessionKey = new Guid(guidBytes).ToString();
cookieValue = CookieProtection.Protect(_dataProtector, sessionKey);
var establisher = new SessionEstablisher(context, cookieValue, _options);
tryEstablishSession = establisher.TryEstablishSession;
isNewSessionKey = true;
}
来源:https://stackoverflow.com/questions/40664567/how-to-determine-session-id-when-using-sql-sever-session-storage