How to Determine Session ID when using SQL Sever session storage

六眼飞鱼酱① 提交于 2020-01-06 15:19:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!