前言

很多童鞋在工作或面试的过程中,也许会遇到这样的问题,使用Session,怎样让多个站点实现Session共享的问题,也就是在A站点登录,那么在B站点就不需要重新登录了那?如果采用Session保存用户信息的话,那么应该怎样解决那?

解决方式之一:使用StateSever

一:sessionState中Mode 的常见三种用法

1. InProc:通常情况下我们都采用这个方法,他表示session值保存在IIS进程中

优点:访问速度快

缺点:易丢失(IIs崩溃、应该程序池超时、重启等都会引起)

2. StateServer:net专门保存Session的一个进程服务中

优点:相对稳定,不易丢失

缺点:访问速度稍慢,毕竟他的值是保存在另外一个进程中。

3. SQLServer:保存在数据中

优点:稳定

缺点:速度慢

二:下面我们就采用第二种方式StateServer解决Session共享问题,使用方法及注意事项如下:

1.将计算机服务"ASP.NET State Service"启动类型改为自动,同时启动改服务。

2.打开注册表,运行cmd/regedit,找到节点HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\aspnet_state\Parameters

  a.将AllowRemoteConnection值设置为1

  [b.将Port值设置为a5b8(十六进制),即十进制42424(默认值)]

  以上根据字面意思就可以看出允许远程连接和设置端口

3.分别在A网站和B网站的配置文件WebConfig 中system.web节点下添加

<sessionState mode="StateServer" stateConnectionString="tcpip=192.168.1.1:42424" timeout="60"></sessionState>

  注意:如果不操作步骤2 或 AllowRemoteConnection=0 那么所有的站点只能在本机  且 stateConnectionString中IP地址必须配置为 localhost:42424或127.0.0.1:42424

4.分别在网站项目A和网站项目B的Global.asax.cs中加入下面代码

        public override void Init()
{
base.Init();
foreach (string moduleName in this.Modules)
{
string appName = "AppName";
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);
}
}
}
}
}

注意两个AppName必须设置为一样,为了让浏览器知道这是一个程序。

以上就是解决Session共享的全部内容,有什么问题,欢迎大家指正。

最新文章

  1. NSDate 时间
  2. Flume日志采集系统——初体验(Logstash对比版)
  3. 只适用于HTML的DOM对象
  4. Linux下Qt的安装与配置
  5. Android(java)学习笔记177:BroadcastReceiver之 应用程序安装和卸载 的广播接收者
  6. 附加、分离数据库和备份、还原数据库的区别(转载于中雪的BLOG)
  7. 做10年Windows程序员与做10年Linux程序员的区别(附无数评论)(开源软件相当于熟读唐诗三百首,不会作诗也会吟)
  8. K-means clustering (K-means聚类)
  9. 【学习】ie8支持rgba()透明度颜色
  10. Django-路由控制
  11. Python爬虫入门教程 14-100 All IT eBooks多线程爬取
  12. PAT B1023
  13. GPU安装小结
  14. 为什么一刷新页面session没了
  15. Word,excel开发指南
  16. Java:运算符的问题
  17. hydra 安装和使用
  18. Hadoop守护进程【简--】
  19. ETL设计详解
  20. nvidia 驱动下载地址

热门文章

  1. 3dsmax2012卸载/安装失败/如何彻底卸载清除干净3dsmax2012注册表和文件的方法
  2. 那些H5用到的技术(4)——弹幕
  3. vue过渡效果
  4. LinuxShell脚本编程7-for和while
  5. 1.6 js基础
  6. WPF与Winform中的不同(1)
  7. Gradient descent and others
  8. 在Eclipse中生成javadoc
  9. 使用webgl(three.js)创建3D机房(升级版)-普通机房
  10. Golang教程:goroutine协程