用ILSpy查看Session.SessionID的生成算法

  

缘由

  asp.net Session在InProc模式下,容易丢失,经常需要重新登录,且不支持分布式共享。  

所以在研究Redis实现原生的Session,本来想用GUID作为key存入cookie,又在想能不能实现跟Session一样的id

 

实现

  ILSpy 是一个开源的.NET反编译工具,简洁强大易用是它的特征。在绝大多数情况下,它都能很好的完成你对未知程序集内部代码的探索。

ILSpy 下载地址:点击下载

  在VS中可以得知SessionID是System.Web.SessionState命名空间的HttpSessionState类下的属性。

在ILSpy中搜索HttpSessionState,找到Session属性,是IHttpSessionState接口对象创建的。

 

   查看HttpSessionState的构造函数,原来IHttpSessionState是从构造函数传过来的。

 

  本来是想通过搜索构造函数找到是哪传过来这个对象的,只是ILSpy只提供类型,成员,常量的搜索

折腾了好久,终于找到IHttpSessionState的接口实现类HttpSessionStateContainer

  DelayedGetSessionId方法

 

  CreateSessionId方法

 

  在追踪CreateSessionID方法,是个接口中定义的方法

 

  搜索CreateSessionID方法,找到具体的现实

 

  最后找到Create方法的所在类

 

测试

   具体的代码:

internal static class SessionId
    {
        internal const int NUM_CHARS_IN_ENCODING = 32;
        internal const int ENCODING_BITS_PER_CHAR = 5;
        internal const int ID_LENGTH_BITS = 120;
        internal const int ID_LENGTH_BYTES = 15;
        internal const int ID_LENGTH_CHARS = 24;
        private static char[] s_encoding;
        private static bool[] s_legalchars;
        internal static bool IsLegit(string s)
        {
            if (s == null || s.Length != 24)
            {
                return false;
            }
            bool result;
            try
            {
                int num = 24;
                while (--num >= 0)
                {
                    char c = s[num];
                    if (!SessionId.s_legalchars[(int)c])
                    {
                        result = false;
                        return result;
                    }
                }
                result = true;
            }
            catch (IndexOutOfRangeException)
            {
                result = false;
            }
            return result;
        }
        internal static string Create(ref RandomNumberGenerator randgen)
        {
            if (randgen == null)
            {
                randgen = new RNGCryptoServiceProvider();
            }
            byte[] array = new byte[15];
            randgen.GetBytes(array);
            return SessionId.Encode(array);
        }
        static SessionId()
        {
            SessionId.s_encoding = new char[]
            {
                'a',
                'b',
                'c',
                'd',
                'e',
                'f',
                'g',
                'h',
                'i',
                'j',
                'k',
                'l',
                'm',
                'n',
                'o',
                'p',
                'q',
                'r',
                's',
                't',
                'u',
                'v',
                'w',
                'x',
                'y',
                'z',
                '0',
                '1',
                '2',
                '3',
                '4',
                '5'
            };
            SessionId.s_legalchars = new bool[128];
            for (int i = SessionId.s_encoding.Length - 1; i >= 0; i--)
            {
                char c = SessionId.s_encoding[i];
                SessionId.s_legalchars[(int)c] = true;
            }
        }
        private static string Encode(byte[] buffer)
        {
            char[] array = new char[24];
            int num = 0;
            for (int i = 0; i < 15; i += 5)
            {
                int num2 = (int)buffer[i] | (int)buffer[i + 1] << 8 | (int)buffer[i + 2] << 16 | (int)buffer[i + 3] << 24;
                int num3 = num2 & 31;
                array[num++] = SessionId.s_encoding[num3];
                num3 = (num2 >> 5 & 31);
                array[num++] = SessionId.s_encoding[num3];
                num3 = (num2 >> 10 & 31);
                array[num++] = SessionId.s_encoding[num3];
                num3 = (num2 >> 15 & 31);
                array[num++] = SessionId.s_encoding[num3];
                num3 = (num2 >> 20 & 31);
                array[num++] = SessionId.s_encoding[num3];
                num3 = (num2 >> 25 & 31);
                array[num++] = SessionId.s_encoding[num3];
                num2 = ((num2 >> 30 & 3) | (int)buffer[i + 4] << 2);
                num3 = (num2 & 31);
                array[num++] = SessionId.s_encoding[num3];
                num3 = (num2 >> 5 & 31);
                array[num++] = SessionId.s_encoding[num3];
            }
            return new string(array);
        }
    }

  调用:

 public partial class Contact : Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            string SessionId = CreateSessionID(Context);
            Response.Write(SessionId+"<br/>");
       Response.Write(Session.SessionID); Response.End(); }
private RandomNumberGenerator _randgen; public virtual string CreateSessionID(HttpContext context) { return SessionId.Create(ref this._randgen); } }

 效果截图:

 

转载于:https://www.cnblogs.com/haomo/p/5764480.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值