<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tony Williams &#187; Asp.Net</title>
	<atom:link href="http://blog.tonywilliams.me.uk/tag/asp-net/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.tonywilliams.me.uk</link>
	<description></description>
	<lastBuildDate>Tue, 10 Jan 2012 12:31:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Share sessions between asp.net apps</title>
		<link>http://blog.tonywilliams.me.uk/share-sessions-between-asp-net-apps/</link>
		<comments>http://blog.tonywilliams.me.uk/share-sessions-between-asp-net-apps/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 08:31:34 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Asp.Net]]></category>
		<category><![CDATA[Session]]></category>

		<guid isPermaLink="false">http://blog.tonywilliams.me.uk/?p=37</guid>
		<description><![CDATA[Share session state between asp.net apps]]></description>
			<content:encoded><![CDATA[<p>From my <a href="http://stackoverflow.com/questions/245947/1271463#1271463">StackOverFlow </a>answer.</p>
<p>If you want to share sessions between different apps there are a few things you need to do.</p>
<p>First you&#8217;ll need to run the session state in SQL mode.<br />
At this point I found out that the SQL session state takes the machine key and your _appDomainAppId to generate a key for your app to access it&#8217;s own session data. So we need to keep these the same between all your apps.</p>
<p>In the web configs of your apps you&#8217;ll need to use the same machine key. This can be any where inside the system.web tags E.G:</p>
<pre class="brush:html">    &lt;machineKey decryptionKey="EDCDA6DF458176504BBCC720A4E29348E252E652591179E2" validationKey="CC482ED6B5D3569819B3C8F07AC3FA855B2FED7F0130F55D8405597C796457A2F5162D35C69B61F257DB5EFE6BC4F6CEBDD23A4118C4519F55185CB5EB3DFE61"/&gt;</pre>
<p>Add an appSetting &#8220;ApplicationName&#8221; and give it name (this has to be the same for both apps)<br />
You&#8217;ll then need to create a shared session module which will change the _appDomainAppId. The one below is what I use.</p>
<pre class="brush:c#">    namespace YourApp
{
  using System.Configuration;
  using System.Reflection;
  using System.Web;

  /// &lt;summary&gt;class used for sharing the session between app domains&lt;/summary&gt;
  public class SharedSessionModule : IHttpModule
  {
    #region IHttpModule Members
    /// &lt;summary&gt;
    /// Initializes a module and prepares it to handle requests.
    /// &lt;/summary&gt;
    /// &lt;param name="context"&gt;An &lt;see cref="T:System.Web.HttpApplication"/&gt;
    /// that provides access to the methods,
    /// properties, and events common to all application objects within an ASP.NET
    /// application&lt;/param&gt;
    /// &lt;created date="5/31/2008" by="Peter Femiani"/&gt;
    public void Init(HttpApplication context)
    {
      // Get the app name from config file...
      string appName = ConfigurationManager.AppSettings["ApplicationName"];
      if (!string.IsNullOrEmpty(appName))
      {
        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, appName);
      }
    }

    /// &lt;summary&gt;
    /// Disposes of the resources (other than memory) used by the module that
    /// implements &lt;see cref="T:System.Web.IHttpModule"/&gt;.
    /// &lt;/summary&gt;
    /// &lt;created date="5/31/2008" by="Peter Femiani"/&gt;
    public void Dispose()
    {
    }
    #endregion
  }
}</pre>
<p>In the web config you&#8217;ll need to add this module:</p>
<pre class="brush:html">      &lt;add name="SharedSessionModule" type="YourApp.SharedSessionModule, YourApp, Version=1.0.0.0, Culture=neutral" /&gt;</pre>
<p>Final thing to do is to allow the session cookie to pass between domains&#8230;like so</p>
<pre class="brush:c#">  var session = HttpContext.Current.Session;
  var request = HttpContext.Current.Request;
  var cookie = request.Cookies["ASP.NET_SessionId"];
  if (cookie != null &amp;&amp; session != null &amp;&amp; session.SessionID != null)
  {
    cookie.Value = session.SessionID;
    cookie.Domain = "yourappdomain.com";

    // the full stop prefix denotes all sub domains
    cookie.Path = "/"; // default session cookie path root
  }</pre>
<p>And that should do the trick.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tonywilliams.me.uk/share-sessions-between-asp-net-apps/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

