Session 共享要注意两点:

1、必须在同一个域名下

2、StateServer模式是把session保存在同一台服务器上的进程:aspnet_state.exe里面,当然也可以保存在memcache和数据库里,这个下一节再讲。

web.config设置:

 <machineKey decryptionKey="FD69B2EB9A11E3063518F1932E314E4AA1577BF0B824F369" validationKey="5F32295C31223A362286DD5777916FCD0FD2A8EF882783FD3E29AB1FCDFE931F8FA45A8E468B7A40269E50A748778CBB8DB2262D44A86BBCEA96DCA46CBC05C3" validation="SHA1" decryption="Auto"/>
<sessionState cookieless="false" timeout="50" mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424"/>
<httpModules>
<add name="CrossDomainCookieModule" type="Entity.SessionProviderHttpModule, Entity"/>
</httpModules>

tcpid的42424是默认端口

统一sessionid


要想共享session,就得把sessionid统一,sessionid是保存在cookie里ASP.NET_SessionId

public class SessionProviderHttpModule : IHttpModule
{
private string m_RootDomain = string.Empty; public void Dispose()
{ } public void Init(HttpApplication context)
{
Sites sites = new Sites();
m_RootDomain = sites.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++)
{
if (app.Context.Response.Cookies[i].Name == "ASP.NET_SessionId")
{
app.Context.Response.Cookies[i].Domain = m_RootDomain;
}
}
}
}
}

这样就可以实现session共享了。

Session 共享(StateServer模式)(原创)的更多相关文章

  1. Session中StateServer的使用方法

    最近项目中用到 Session的StateServer模式,我们知道sessionState有四种模式:off,inProc,StateServer,SqlServer. 而StateServer 是 ...

  2. ASP.NET 使用mode=”InProc”方式保存Session老是丢失,无奈改成StateServer 模式。

    http://blog.csdn.net/fox123871/article/details/8165431 session是工作在你的应用程序进程中的.asp.net进程.iis往往会在20分钟之后 ...

  3. 关于多台机器之前session共享,sessionState mode="StateServer" 问题的困扰

    .net 多台机器共享session是很老的技术,一直很少用到session. 最近就出现了一个问题:三台前端,其中一台保存的session值死活不对,一样的环境,一样的配置文件,就是和另外两台获得的 ...

  4. ASP.NET 负载均衡 StateServer Session共享问题(经验记录)

    (源地址:http://www.cnblogs.com/ryhan/p/3748976.html) 最近在改造公司的一个系统 支持F5硬件负载,由于系统后面还跟了个异步工具,需要将Admin上传的文件 ...

  5. 【原创】搭建Nginx(负载均衡)+Redis(Session共享)+Tomcat集群

    为什么移除首页?哪里不符合要求?倒是回我邮件啊! 一.环境搭建 Linux下Vagrant搭建Tomcat7.Java7 二.Nginx的安装配置与测试 *虚拟机下转至root sudo -i 1)下 ...

  6. keepalived+nginx+tomcat+redis实现负载均衡和session共享(原创)

    keepalived+nginx+tomcat+redis实现负载均衡和session共享 直接上链接,码了一天,就不再重写了,希望能帮到大家,有问题欢迎留言交流.

  7. 分布式中使用Redis实现Session共享(二)

    上一篇介绍了一些redis的安装及使用步骤,本篇开始将介绍redis的实际应用场景,先从最常见的session开始,刚好也重新学习一遍session的实现原理.在阅读之前假设你已经会使用nginx+i ...

  8. 分布式中使用Redis实现Session共享(一)

    上一篇介绍了如何使用nginx+iis部署一个简单的分布式系统,文章结尾留下了几个问题,其中一个是"如何解决多站点下Session共享".这篇文章将会介绍如何使用Redis,下一篇 ...

  9. Session共享的解决方案

    http://www.cnblogs.com/xinhaijulan/archive/2010/08/21/1805116.html Session共享的解决方案 1.客户端SessionID值唯一: ...

随机推荐

  1. python开发_tkinter_复选菜单

    在之前的blog中有提到python的tkinter中的菜单操作 python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐 python开发_tkinter_窗口控件_自 ...

  2. Codeforces Round #294 (Div. 2)D - A and B and Interesting Substrings 字符串

    D. A and B and Interesting Substrings time limit per test 2 seconds memory limit per test 256 megaby ...

  3. Codecademy python

    #1 print "Welcome to Python!" #2 my_variable = #3 # Set the variables to the values listed ...

  4. Codeforces Round #234 (Div. 2) A. Inna and Choose Options 模拟题

    A. Inna and Choose Options time limit per test 1 second memory limit per test 256 megabytes input st ...

  5. ROWID面试题-删除表中重复数据(重复数据保留一个)

    /* ROWID是行ID,通过它一定可以定位到r任意一行的数据记录 ROWID DNAME DEPTNO LOC ------------------ ------------------------ ...

  6. python+Django+apache的配置

    下载安装xampp套件 下载mod_python-3.3.1.win32-py2.5-Apache2.2.exe 下载python-2.5.4.msi 下载Django 下载MySQL-python- ...

  7. 淘宝API得到单个商品的信息,用了淘宝的SDK...

    淘宝api获取到的数据.返回结果可以选择json格式和xml格式的啊.每个api下面都有sdk调用示例哦. 详细:http://wenwen.soso.com/z/q335640192.htm 淘宝开 ...

  8. [Mysql]MySQL 服务无法启动。

    摘要 在官网下载了mysql,版本mysql-5.7.17-winx64,免安装的压缩包,解压后.放在MySql的文件夹中.电脑系统win10 x64. 配置文件 # For advice on ho ...

  9. jvm执行流程

    首先给一个简单的Java示例,源代码如下: public class Main { private static int size=1; public static void main(String  ...

  10. Tomcat – java.lang.OutOfMemoryError: PermGen space Cause and Solution

    Read more: http://javarevisited.blogspot.com/2012/01/tomcat-javalangoutofmemoryerror-permgen.html#ix ...