asp.net 二级域名session共享

1.自定义类

namespace SessionShare
{
public class CrossDomainCookie : IHttpModule
{
private string m_RootDomain = string.Empty;
#region IHttpModule Members
public void Dispose()
{
}
public void Init(HttpApplication context)
{
m_RootDomain = ConfigurationManager.AppSettings["RootDomain"];
Type stateServerSessionProvider = typeof(HttpSessionState).Assembly.GetType("System.Web.SessionState.OutOfProcSessionStateStore");
FieldInfo uriField = stateServerSessionProvider.GetField("s_uribase", BindingFlags.Static | BindingFlags.NonPublic);
if (uriField == null)
throw new ArgumentException("UriField was not found");
uriField.SetValue(null, m_RootDomain);
context.EndRequest += new System.EventHandler(context_EndRequest);
}
void context_EndRequest(object sender, System.EventArgs e)
{
HttpApplication app = sender as HttpApplication;
for (int i = 0; i < app.Context.Response.Cookies.Count; i++)
{
app.Context.Response.Cookies[i].Domain = m_RootDomain;
}
}
#endregion
}
}
2.web.config中添加如下内容
<appSettings>
    <!--Session共享域名-->
    <add key="RootDomain" value=".session.com"/>
  </appSettings>
<system.web>
    <machineKey decryptionKey="FD69B2EB9A11E3063518F1932E314E4AA1577BF0B824F369" validationKey="5F32295C31223A362286DD5777916FCD0FD2A8EF882783FD3E29AB1FCDFE931F8FA45A8E468B7A40269E50A748778CBB8DB2262D44A86BBCEA96DCA46CBC05C3" validation="SHA1" decryption="Auto"/>
    <sessionState cookieless="false" timeout="50" mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424"/>
<httpModules>
            <add name="CrossDomainCookieModule" type="SessionShare.CrossDomainCookie,SessionShare"/>
    </httpModules>
</system.web>
<system.webServer>
<modules>
            <add name="CrossDomainCookieModule" type="SessionShare.CrossDomainCookie,SessionShare"/>
    </modules>
</system.webServer>
3.修改C:\WINDOWS\system32\drivers\etc\hosts文件(hosts文件做ip映射)
注:两个网站都要添加.

 

posted @ 2015-04-11 23:43  甜菜波波  阅读(398)  评论(0编辑  收藏  举报