ASP. NET second-level domain name site shared Session Status, asp. netsession

Source: Internet
Author: User

ASP. NET second-level domain name site shared Session Status, asp. netsession

The previous article mentioned how to share the login status between second-level sites using ASP. NET form authentication,

Http://www.cnblogs.com/jzywh/archive/2007/09/23/902905.html,

Today, I want to write about how to share a Session between a second-level domain name site and a second-level domain name site.

 

First, the Session must be shared, and the sessionids between sites must be consistent. How can we ensure that sessionids are consistent?

ASP. the SessionID in. NET is the key value of the cookie stored on the client as ASP. NET_SessionId is a string used to maintain the relationship between the viewer sessions. To share the SessionID between the primary site and the secondary domain site, we must first implement ASP. share the NET_SessionId cookie.

CrossDomainCookieModule

Bytes ------------------------------------------------------------------------------------------------

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"];            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 }

 

 

 

The Module above resets the domain of all cookies to the root domain, and the root domain is set in web. config. Someone may say that this is the domain where all cookies are overwritten. Then, he can determine the cookie name. If it is ASP. NET_SessionId, the domain name is overwritten.

 

If the master site and the second-level domain name site are the same site, then you will share the session because the Session ID is the same and the Session container is the same.

 

If the primary site and secondary domain site are two different sites, more operations are required.

 

If the two sites are different servers, the solution should be simple:

1) use the same state server to store sessions.

2) set the same machineKey in the web. config of the two sites.

MachineKey settings see http://msdn.microsoft.com/zh-cn/asp.net/w8h3skw9.aspx

3) set the same name for the two sites

This is done to ensure that the siteID of the two sites is the same. siteID is the hash value of site name. Do not use the default site because the siteID of the default site is not the hash of site name.

 

If the two sites are on the same server, you need to modify the CrossDomainCookie. This method can also be used when the two sites are on different servers:

1) use the same state server to store sessions.

2) use reflection to set the static field s_uribase value of System. Web. SessionState. OutOfProcSessionStateStore

 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    }

 

 

After such modification, you can share the Session.

 

Similarly, if you use SQL server to store sessions, you can use a similar method to solve the Session sharing problem.

 

Sample File Download http://files.cnblogs.com/jzywh/ShareSessionSample.zip

Source: http://jzywh.cnblogs.com

Related Article

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.