自定义 Asp.Net SessionID 获取方式

新建类 CustomSessionIDManagerweb

    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.Configcookie

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

 

 

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