{"id":2634,"date":"2013-04-25T06:55:58","date_gmt":"2013-04-25T06:55:58","guid":{"rendered":"http:\/\/enjoyasp.net\/?p=2634"},"modified":"2013-09-20T00:53:16","modified_gmt":"2013-09-20T00:53:16","slug":"%e4%ba%8c%e7%ba%a7%e5%9f%9f%e5%90%8dsession%e5%85%b1%e4%ba%ab","status":"publish","type":"post","link":"https:\/\/enjoyasp.net\/index.php\/2013\/04\/25\/%e4%ba%8c%e7%ba%a7%e5%9f%9f%e5%90%8dsession%e5%85%b1%e4%ba%ab\/","title":{"rendered":"\u4e8c\u7ea7\u57df\u540dsession\u5171\u4eab"},"content":{"rendered":"<div>\n<pre class=\"brush:sql;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;\">\r\n\u4e00\u3001\u5b9e\u73b0IHttpModule\u7c7b\uff0c\u89c1\u540e\u3002\r\n\r\n\u4e8c\u3001\u4fee\u6539web.config\r\n\u6dfb\u52a0\u57df\u540d\r\n&lt;appSettings&gt;&lt;add key=&quot;RootDomain&quot; value=&quot;&quot;\/&gt;&lt;\/appSettings&gt;\r\nRootDomain cookie\u57df\u540d\uff0c\u82e5\u662f\u672c\u5730\u5219\u5199\u7a7a\r\nby design domain names must have at least two dots otherwise browser will say they are invalid \r\nwhen working on localhost (!) the cookie-domain must be set to &quot;&quot; or NULL or FALSE instead of &quot;localhost&quot;\r\n\r\n\u589e\u52a0\u8fc7\u6ee4\r\n&lt;system.web&gt;\r\n&lt;sessionState mode=&quot;StateServer&quot; stateNetworkTimeout=&quot;10&quot; timeout=&quot;120&quot; stateConnectionString=&quot;tcpip=127.0.0.1:42424&quot; \/&gt;\r\n &lt;httpModules&gt;\r\n\r\n      &lt;add name=&quot;SessionSharedHttpModule&quot; type=&quot;ClassLibrary.BasePage.MakeSessionIDOneOnly,ClassLibrary&quot;\/&gt;\r\n\r\n    &lt;\/httpModules&gt;\r\n&lt;\/system.web&gt;\r\n\r\n\u4e09\u3001\u9644\u7c7b\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Web;\r\nusing System.Web.SessionState;\r\nusing System.Reflection;\r\nusing System.Configuration;\r\n\r\nnamespace ClassLibrary.BasePage\r\n{\r\n    public class MakeSessionIDOneOnly : IHttpModule\r\n    {\r\n        private string m_RootDomain = string.Empty;\r\n\r\n        #region IHttpModule Members\r\n\r\n        public void Dispose()\r\n        {\r\n\r\n        }\r\n\r\n        public void Init(HttpApplication context)\r\n        {\r\n            m_RootDomain = ConfigurationManager.AppSettings[&quot;RootDomain&quot;];\r\n\r\n            Type stateServerSessionProvider = typeof(HttpSessionState).Assembly.GetType(&quot;System.Web.SessionState.OutOfProcSessionStateStore&quot;);\r\n            FieldInfo uriField = stateServerSessionProvider.GetField(&quot;s_uribase&quot;, BindingFlags.Static | BindingFlags.NonPublic);\r\n\r\n            if (uriField == null)\r\n                throw new ArgumentException(&quot;UriField was not found&quot;);\r\n\r\n            uriField.SetValue(null, m_RootDomain);\r\n\r\n            context.EndRequest += new System.EventHandler(context_EndRequest);\r\n        }\r\n\r\n        void context_EndRequest(object sender, System.EventArgs e)\r\n        {\r\n            HttpApplication app = sender as HttpApplication;\r\n            for (int i = 0; i &lt; app.Context.Response.Cookies.Count; i++)\r\n            {\r\n                if (app.Context.Response.Cookies[i].Name == &quot;ASP.NET_SessionId&quot;)\r\n                {\r\n                    app.Context.Response.Cookies[i].Domain = m_RootDomain;\r\n                }\r\n            }\r\n        }\r\n\r\n        #endregion\r\n    }\r\n}\r\n\r\n\r\n\r\n\r\n\r\n\r\n<\/pre>\n<\/div>\n<p>\n<span style=\"font-size:12px;\">\u53c2\u8003\uff1a<a href=\"http:\/\/www.cnblogs.com\/xinhaijulan\/archive\/2010\/08\/21\/1805116.html\">Session\u5171\u4eab\u7684\u89e3\u51b3\u65b9\u6848<\/a><\/span> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5b9e\u73b0IHttpModule\u7c7b\uff0c\u89c1\u540e\u3002 \u4e8c\u3001\u4fee\u6539web.config \u6dfb\u52a0\u57df\u540d &lt;appSetting [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-2634","post","type-post","status-publish","format-standard","hentry","category-aspdotnet"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/2634","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/comments?post=2634"}],"version-history":[{"count":0,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/2634\/revisions"}],"wp:attachment":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/media?parent=2634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/categories?post=2634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/tags?post=2634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}