如何在使用SQL Sever会话存储时确定会话ID

时间:2016-11-17 20:39:46

标签: c# asp.net session asp.net-core asp.net-core-mvc

我跟着this guide设置了Sql Server作为Session数据的存储。

我注意到Sessions表有一个ID列。我希望我可以使用此ID列来确定正在使用的当前会话(Sessions表中的行)。但是,我不知道如何生成此ID,或者幕后的ASP.NET Core如何将此ID与Session匹配。

我尝试使用HttpContext.Session.Id,但它与数据库中的ID不同。

那么,我如何确定会话中使用哪一行?

1 个答案:

答案 0 :(得分:0)

随机生成。您可以在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;
        }
相关问题