代码之家  ›  专栏  ›  技术社区  ›  Erica Stockwell-Alpert

共享会话状态不工作;2ASP.NET_会话ID饼干?

  •  0
  • Erica Stockwell-Alpert  · 技术社区  · 7 年前

    我有两个网站,sub1。topleveldomain.com网站和sub2。topleveldomain.com网站我正在尝试为实现共享会话状态以启用SSO。我遵循了这里的文档:

    https://support.microsoft.com/en-us/help/2527105/how-to-share-session-state-across-subdomains

    两个web配置都具有以下设置:

    <sessionState mode="SQLServer" allowCustomSqlDatabase="true" sqlConnectionString="Data Source=sqlserver;user id=user;password=password;Application Name=MyAppName" cookieless="false" />
    
    <machineKey validationKey="[same machine key]" decryptionKey="[same decryption key]" validation="SHA1" decryption="AES" />    
    
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
            <add name="SharedSessionModule" type="CSASPNETShareSessionBetweenSubDomainsModule.SharedSessionModule, CSASPNETShareSessionBetweenSubDomainsModule, Version=1.0.0.0, Culture=neutral"/>
        </modules>
    </system.webServer>
    
    <appSettings>
        <add key="ApplicationName" value="MyAppName"/>
        <add key="RootDomain" value="topleveldomain.com"/>
    </appSettings>
    

    <client>  
        <endpoint address="https://sub1.topleveldomain.com/Services/UserService.svc" binding="basicHttpsBinding" bindingConfiguration="BasicHttpsBinding_IUserService" contract="netzkern.SC.UserManager.Contracts.IUserService" name="BasicHttpsBinding_IUserService" />
    </client>
    

    两个站点都能正常加载和工作,但是当我登录到sub1时,我不能登录到sub2。我检查了饼干,有两个ASP.Net_会话ID饼干。这会是问题吗?如果是问题,我需要在这个代码中更改什么?

    enter image description here

    public class SharedSessionModule : IHttpModule
    {
        // Cache settings on memory. 
        protected static string applicationName = ConfigurationManager.AppSettings["ApplicationName"];
        protected static string rootDomain = ConfigurationManager.AppSettings["RootDomain"];
    
        #region IHttpModule Members 
        /// <summary> 
        /// Initializes a module and prepares it to handle requests. 
        /// </summary> 
        /// <param name="context"> 
        /// An System.Web.HttpApplication 
        /// that provides access to the methods, 
        /// properties, and events common to all application objects within  
        /// an ASP.NET application. 
        /// </param> 
        public void Init(HttpApplication context)
        {
            // This module requires both Application Name and Root Domain to work. 
            if (string.IsNullOrEmpty(applicationName) ||
                string.IsNullOrEmpty(rootDomain))
            {
                return;
            }
    
            // Change the Application Name in runtime. 
            FieldInfo runtimeInfo = typeof(HttpRuntime).GetField("_theRuntime",
                BindingFlags.Static | BindingFlags.NonPublic);
            HttpRuntime theRuntime = (HttpRuntime)runtimeInfo.GetValue(null);
            FieldInfo appNameInfo = typeof(HttpRuntime).GetField("_appDomainAppId",
                BindingFlags.Instance | BindingFlags.NonPublic);
    
            appNameInfo.SetValue(theRuntime, applicationName);
    
            // Subscribe Events. 
            context.PostRequestHandlerExecute += new EventHandler(context_PostRequestHandlerExecute);
        }
    
        /// <summary> 
        /// Disposes of the resources (other than memory) used by the module 
        /// that implements. 
        /// </summary> 
        public void Dispose()
        {
        }
        #endregion
    
        /// <summary> 
        /// Before sending response content to client, change the Cookie to Root Domain 
        /// and store current Session Id. 
        /// </summary> 
        /// <param name="sender"> 
        /// An instance of System.Web.HttpApplication that provides access to 
        /// the methods, properties, and events common to all application 
        /// objects within an ASP.NET application. 
        /// </param> 
        void context_PostRequestHandlerExecute(object sender, EventArgs e)
        {
            try
            {
                HttpApplication context = (HttpApplication) sender;
    
                // ASP.NET store a Session Id in cookie to specify current Session. 
                HttpCookie cookie = context.Response.Cookies["ASP.NET_SessionId"];
    
                if (context.Session != null &&
                    !string.IsNullOrEmpty(context.Session.SessionID))
                {
                    // Need to store current Session Id during every request. 
                    cookie.Value = context.Session.SessionID;
    
                    // All Applications use one root domain to store this Cookie 
                    // So that it can be shared. 
                    if (rootDomain != "localhost")
                    {
                        cookie.Domain = rootDomain;
                    }
    
                    // All Virtual Applications and Folders share this Cookie too. 
                    cookie.Path = "/";
                }
            }
            catch (Exception ex)
            {
                return;
            }
        }
    }
    
    0 回复  |  直到 7 年前
    推荐文章