Here we will explain how to solve the problem to share session between Asp.net Web application.
I have two exactly the same web sites in different language hosted under IIS. ASP.NET state service is running on my machine and the web.config is updated in both sites with the following code:
Step 1: Make sure ASP .NET state service is running. Start the state service following the given steps
Open a run box.
Type services.msc and press Ok.
Start Asp.Net State Service
Step 2 : Application name must be matched. Add the key under App Setting in Web.Config on both website.
<appSettings>
<add key="ApplicationName" value="Test" />
</<appSettings>
Step 3 : Add the machineKey in Web.Config on both website.
<machineKey validationKey="7CB8DF6872FB6B35DECD3A8F55582350FEE1FAB9BE6B930216056C1B5BA69A4C5777B3125A42C4AECB4419D43EC12F168FD1BB887469798093C3CAA2427B2B89"
decryptionKey="02FC52E4C71544868EE297826A613C53537DF8FDAF93FA2C64E9A5EF0BA467FB"
validation="SHA1" decryption="AES"/>
Step 4 : Add sessionState in Web.Config on both website.
<sessionState cookieless="UseCookies" mode="StateServer" regenerateExpiredSessionId="false" timeout="120">
<providers>
<clear />
</providers>
</sessionState>
Step 5 : Add below code in Application setting file (Global.asax)
public override void Init()
{
base.Init();
try
{
// Get the app name from config file...
string appName = ConfigurationManager.AppSettings["ApplicationName"];
if (!string.IsNullOrEmpty(appName))
{
foreach (string moduleName in this.Modules)
{
IHttpModule module = this.Modules[moduleName];
SessionStateModule ssm = module as SessionStateModule;
if (ssm != null)
{
FieldInfo storeInfo = typeof(SessionStateModule).GetField("_store", BindingFlags.Instance | BindingFlags.NonPublic);
SessionStateStoreProviderBase store = (SessionStateStoreProviderBase)storeInfo.GetValue(ssm);
if (store == null) //In IIS7 Integrated mode, module.Init() is called later
{
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);
}
else
{
Type storeType = store.GetType();
if (storeType.Name.Equals("OutOfProcSessionStateStore"))
{
FieldInfo uribaseInfo = storeType.GetField("s_uribase", BindingFlags.Static | BindingFlags.NonPublic);
uribaseInfo.SetValue(storeType, appName);
}
}
}
}
}
}
catch (Exception ex)
{
string s = ex.Message.ToString();
}
}
After doing the above steps now you are able to share sessions between Asp.Net applications.