使用SQL Sever会话存储时如何确定会话ID [英] How to Determine Session ID when using SQL Sever session storage

查看:115
本文介绍了使用SQL Sever会话存储时如何确定会话ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了本指南以将Sql Server设置为我的Session数据存储.

I followed this guide to set up Sql Server as my store for Session data.

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

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.

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

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?

推荐答案

它是随机生成的.您可以在中间件源

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;
        }

这篇关于使用SQL Sever会话存储时如何确定会话ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆