使用 Redis 共享 Session 会话
储存模式
1.InProc模式
这是ASP.NET默认的Session管理模式,在应用进程内维护Session。
2.StateServer模式
这是在服务器装了.NET环境后自带的一个StateServer服务,在应用进程外管理Session,可以进行多应用间的Session共享。
3.SQLServer模式
这是利用SQLServer进行Session的托管。其优点在于可以利用SQLServer的优势处理海量Session,在应用进程外、可持久化、安全性高等优点。
4.Custom模式
这是自定义模式,发挥空间很大,在拥有Provider的情况下,可以利用这一模式进行发挥,利用各种各样的数据存储程序进行Session管理。
InProc模式是默认的使用模式比较适合单应用。
StateServer模式还需要配置注册表;
SQLServer模式使用MS的DB;如果是多台服务器还是有SessionID的问题;缺点都是使用MS定制,不能跨语言。Custom模式 比较适合结合第三方的Provider如Redis,Memecache,Mongodb
适用与多台服务负载Session共享的场景,因为MVC下本身木有viewstate这些,不需要配置machinekey,如果使用SqlServer或StateServer会话模式解决SessionID不一致的问题(之前是反射修改一致或创建同样的ID)
基于Redis实现
因为基于Redis的实现Harbour.RedisSessionStateStore依赖于ServiceStack.Redis 说是4.x的版本有请求数限制,所以选择了Microsoft.Web.RedisSessionStateProvider,使用也比较简单
PM> Install-Package Microsoft.Web.RedisSessionStateProvider
<sessionState mode="Custom" customProvider="RedisSessionStateStore">
<providers>
<!-- Either use 'connectionString' and provide all parameters as string OR use 'host','port','accessKey','ssl','connectionTimeoutInMilliseconds' and 'operationTimeoutInMilliseconds'. -->
<!-- 'throwOnError','retryTimeoutInMilliseconds','databaseId' and 'applicationName' can be used with both options. -->
<!--
<add name="RedisSessionStateStore"
host = "127.0.0.1" [String]
port = "" [number]
accessKey = "" [String]
ssl = "false" [true|false]
throwOnError = "true" [true|false]
retryTimeoutInMilliseconds = "5000" [number]
databaseId = "0" [number]
applicationName = "" [String]
connectionTimeoutInMilliseconds = "5000" [number]
operationTimeoutInMilliseconds = "1000" [number]
connectionString = "<Valid StackExchange.Redis connection string>" [String]
loggingClassName = "<Assembly qualified class name that contains logging method specified below>" [String]
loggingMethodName = "<Logging method should be defined in loggingClass. It should be public, static, does not take any parameters and should have a return type of System.IO.TextWriter.>" [String]
/>
-->
<add name="RedisSessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" host="xxx.xxx.xxx.xxx" accessKey="" ssl="false" />
</providers>
</sessionState>
另外确保配置了Cookies的Domain
<httpCookies domain=".xxx.com" httpOnlyCookies="true"/>
销毁Session
/// <summary>
/// 用户登出
/// </summary>
/// <param name="returnUrl"></param>
/// <returns></returns>
public ActionResult SignOut(string returnUrl)
{
//清除Session
Session.Abandon();
Session.Clear();
//清除自动登录Cookie
var cookie = Request.Cookies[ConstantKey.autoLoginKey];
if (cookie != null)
{
cookie.Domain = ".xxx.com";
cookie.HttpOnly = true;
cookie.Expires = DateTime.Now.AddDays(-);
Response.AppendCookie(cookie);
}
return RedirectToLocal(returnUrl);
}
!!!其他注意点
SessionId 不一致问题
PRB:如果您使用 SqlServer 或 StateServer 会话模式 Web 场中会丢失会话状态
https://support.microsoft.com/zh-cn/kb/325056
趣谈StateServer在Web Garden,Web Farm下的使用
www.cnblogs.com/UliiAn/p/3463720.html
ASP.NET 负载均衡 StateServer Session共享问题
http://www.cnblogs.com/ryhan/p/3748976.html
Cookies的Domain
<httpCookies domain=".xxx.com" httpOnlyCookies="true"/>
SQLServer模式
http://www.cnblogs.com/haoxue/archive/2010/10/11/asp_net_session_share.html
http://www.cnblogs.com/whyloverjack/archive/2011/11/10/2244719.html
Custom模式Provider
Redis
基于Redis的实现
Harbour.RedisSessionStateStore
https://github.com/TheCloudlessSky/Harbour.RedisSessionStateStore
ASP.NET下跨应用共享Session和使用Redis进行Session托管
http://www.cnblogs.com/UliiAn/p/3554863.html
Microsoft.Web.RedisSessionStateProvider
https://www.nuget.org/packages/Microsoft.Web.RedisSessionStateProvider
https://github.com/welegan/RedisSessionProvider
https://github.com/Azure/aspnet-redis-providers/wiki
Couchbase
http://blog.couchbase.com/introducing-couchbase-aspnet-sessionstate-provider
MongoDB
Custom ASP.NET session state store using MongoDB
http://www.adathedev.co.uk/2011/05/mongodb-aspnet-session-state-store.html
https://github.com/AdaTheDev/MongoDB-ASP.NET-Session-State-Store
Memecache
https://github.com/enyim/memcached-providers
http://www.cnblogs.com/luminji/archive/2011/11/03/2195704.html
AppFabric
https://msdn.microsoft.com/zh-cn/library/ee790859.aspx
http://www.cnblogs.com/xling/archive/2012/07/20/2600439.html
自己实现可以参考:ASP.NET:EntityFramework实现Session
Refer:
Exploring Session in ASP.NEThttp://www.codeproject.com/Articles/32545/Exploring-Session-in-ASP-Net
Announcing ASP.NET Session State Provider for Redis Preview Release
http://blogs.msdn.com/b/webdev/archive/2014/05/12/announcing-asp-net-session-state-provider-for-redis-preview-release.aspxASP.NET Session State Provider for Azure Redis Cache
https://msdn.microsoft.com/en-us/library/azure/dn690522.aspx在Forms验证模式下,实现多个站点(SubDomain相同)共享同一用户登录状态
http://www.cnblogs.com/caomao/archive/2005/07/05/186606.html#3013251ASP.NET二级域名站点共享Session状态
http://www.cnblogs.com/jzywh/archive/2008/11/02/ShareSession.htmlAsp.net 多服务器 Session共享
http://www.cnblogs.com/2814/archive/2012/11/06/2757538.html
使用 Redis 共享 Session 会话的更多相关文章
- Nginx+tomcat集群使用redis共享session
一 :nginx负载均衡 当Tomcat当做独立的Servlet容器来运行时,可看做是能运行Java Servlet的独立Web服务器. 此外 Tomcat还可以作为其他Web服务器进程内或者进程外的 ...
- linux下实现redis共享session的tomcat集群
为了实现主域名与子域名的下不同的产品间一次登录,到处访问的效果,因此采用rediss实现tomcat的集群效果.基于redis能够异步讲缓存内容固化到磁盘上,从而当服务器意外重启后,仍然能够让sess ...
- SpringBoot,Security4, redis共享session,分布式SESSION并发控制,同账号只能登录一次
由于集成了spring session ,redis 共享session,导致SpringSecurity单节点的session并发控制失效, springSession 号称 无缝整合httpses ...
- Tomcat7.0.99集群使用Redis共享session方案
以前配置过给予多播的session共享方案,这回再配置一个redis共享session的. 先小小的炫耀一下: 相信大家要做Tomcat+Redis+session配置,遇到的头号麻烦就是编译的tom ...
- Spring Boot 使用 Redis 共享 Session 代码示例
参考资料 博客:spring boot + redis 实现session共享 1. 新建 Maven 工程 我新建 spring-boot-session-redis maven 工程 2. 引入 ...
- SpringBoot系列: Redis 共享Session
Web项目Session管理是一个很重要的话题, 涉及到系统横向扩展, SpringBoot已经为共享Session很好的解决方案, 这篇文章关注使用Redis共享会话, 同时这也是最常用的方法. = ...
- ASP.NET MVC 使用Redis共享Session
储存模式 1.InProc模式 这是ASP.NET默认的Session管理模式,在应用进程内维护Session. 2.StateServer模式 这是在服务器装了.NET环境后自带的一个StateSe ...
- 【荐】PHP Session和Cookie,Session阻塞,Session垃圾回收,Redis共享Session,不推荐Memcached保存Session
什么是 Session 在 web 应用开发中,Session 被称为会话.主要被用于保存某个访问者的数据. 由于 HTTP 无状态的特点,服务端是不会记住客户端的,对服务端来说,每一个请求都是全新的 ...
- 一篇文章让你深透理解cookie和session,附带分布式WEB系统redis共享session方案
cookie和session有什么区别?这是一个很基础的知识点,大家可能都知道一个大概:cookie是存在客户端的,session是存储在服务端,cookie和session用来验证识别用户的登录状态 ...
随机推荐
- SQL优化实战之加索引
有朋友和我说他的虚机里面的mysql无法跑sql,但是在本地环境是这个sql是可以跑出来的.碰到这个问题第一反应是:死锁. 于是让他查询数据库的几个状态: 发现连即时锁都非常少,不是锁的问题. 进一步 ...
- 安装postgis,使用postgis导入shapefile的步骤总结
最近在做开源WebGIS方面的工作,要使用postgis导入shapefile数据.难点在安装过程和导入时命令行参数的使用,以下分别作个介绍,希望对大家有点用 一.安装postgis (1)首先到po ...
- HBase thrift2 TIOError
如果HBase thrift2报:"TIOError exception: Default TException", 这个可能是因为操作的表不存在,不一定是网络或磁盘操作异常. H ...
- js常见input校验
//校验输入价格等,保留2位小数 function clearNoNum(obj){ obj.onkeyup = function(event){ var e = event || window.ev ...
- InvocationHandler中invoke方法中的第一个参数proxy的用途
最近在研究Java的动态代理时对InvocationHandler中invoke方法中的第一个参数一直不理解它的用处,某度搜索也搜不出结果,最后终于在stackoverflow上找到了答案. 这是原文 ...
- input的type=file触发的相关事件
与input相关的事件运行的过程.添加了一些相关的方法测试了一下.input的type=file的运行流程. 我们书写了mousedown,mouseup,click,input,change,foc ...
- hdu 3015
这个题给你一堆树,每棵树的位置x和高度h都给你 f[i]代表这棵树的位置排名,s[i]代表这棵树的高度排名 问你任意两棵树的(f[i] - f[j])*min(s[i],s[j])和 (f[i]-f[ ...
- 杭电2133What day is it
给你个日期 问是星期几 知道1 1 1是周1就行了 #include <iostream>#include <cstdio>using namespace std ...
- 在.net中使用ETW事件的方法
直到.net4.5,才有了比较便利的操作ETW的方法. 本文介绍的方法主要来源于Microsoft.Diagnostics.Tracing.TraceEvent官方资料库. 准备 (1)需要用到类:M ...
- linux上安装python2.7.11
好久不玩儿linux了,本来就不熟,现在几乎白痴.步骤如下: 从python官网上下载python的源代码 tar zvxf后得到一个文件夹: 进入Python-2.7.11,按照https://do ...