Cross-subdomain Session sharing in ASP. NET

Source: Internet
Author: User

Address: http://www.itivy.com/ivy/archive/2011/4/24/634392360221984930.html

 

Create a class library project and add the following classes:

 

Using System. Web;
Using System;
Using System. Configuration;
Using System. Diagnostics;
Using System. Reflection;
Using System. Web. SessionState;

Namespace WebLibrary
{
Public class CrossDomainCookie: IHttpModule
{
Private string m_RootDomain = string. Empty;

# Region IHttpModule Members
Public void Dispose ()
{
}
Public void Init (HttpApplication context)
{
M_RootDomain = ConfigurationManager. deleettings ["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
}
}

 

Compile it into a dll.

Then, do the following in each website project that requires session sharing:

1. reference the above dll

2. Add your main site domain name to the appsetting node of web. config.

<Add key = "RootDomain" value = ".itivy.com"/>

3. Add the following content to the system. web node of web. config:

<HttpModules>
<Add name = "CrossDomainCookieModule" type = "WebLibrary. CrossDomainCookie, WebLibrary"/>
</HttpModules>

4. On the system. web node of web. config, change the session storage mode to stateserver.

<SessionState mode = "StateServer" stateConnectionString = "tcpip = 127.0.0.1: 42424" timeout = "30"/>

5. Add the following content to system. webServer of web. config:

<Modules>
<Add name = "CrossDomainCookieModule" preCondition = "managedHandler" type = "WebLibrary. CrossDomainCookie, WebLibrary"/>
</Modules>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.