首页 > 技术文章 > 自定义 Asp.Net SessionID 获取方式

smartstar 2018-10-11 15:46 原文

新建类 CustomSessionIDManager

    public class CustomSessionIDManager : SessionIDManager, ISessionIDManager
    {
        private SessionStateSection pConfig = null;
        string ISessionIDManager.GetSessionID(HttpContext context)
        {
            if (pConfig == null)
            {
                Configuration cfg =
                  WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
                pConfig = (SessionStateSection)cfg.GetSection("system.web/sessionState");
            }

            string sid = base.GetSessionID(context);//默认从Cookie、UseUri 中获取

            if (string.IsNullOrWhiteSpace(sid)
                && !string.IsNullOrWhiteSpace(context.Request.QueryString[pConfig.CookieName]))
            {
                var _sid = context.Request.QueryString[pConfig.CookieName];//从自定义查询字符中获取,也可以扩展从自定义Header中获取
                

                return sid;
            }
            return sid;
        }
    }

 修改Web.Config

  <system.web>    
    <sessionState cookieName="_sid" sessionIDManagerType="命名空间.CustomSessionIDManager" />

 

 

博客地址: https://www.cnblogs.com/smartstar/
博客版权: 本文以学习、研究和分享为主,欢迎转载,但必须在文章页面明显位置给出原文连接。
如果文中有不妥或者错误的地方还望高手的你指出,以免误人子弟。如果觉得本文对你有所帮助不如【推荐】一下!如果你有更好的建议,不如留言一起讨论,共同进步!
再次感谢您耐心的读完本篇文章。

推荐阅读