自定义 Asp.Net SessionID 获取方式

新建类 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" />

猜你喜欢

转载自www.cnblogs.com/smartstar/p/9772830.html