储存模式

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.NET

http://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.aspx

ASP.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#3013251

ASP.NET二级域名站点共享Session状态

http://www.cnblogs.com/jzywh/archive/2008/11/02/ShareSession.html

Asp.net 多服务器 Session共享

http://www.cnblogs.com/2814/archive/2012/11/06/2757538.html

使用 Redis 共享 Session 会话的更多相关文章

  1. Nginx+tomcat集群使用redis共享session

    一 :nginx负载均衡 当Tomcat当做独立的Servlet容器来运行时,可看做是能运行Java Servlet的独立Web服务器. 此外 Tomcat还可以作为其他Web服务器进程内或者进程外的 ...

  2. linux下实现redis共享session的tomcat集群

    为了实现主域名与子域名的下不同的产品间一次登录,到处访问的效果,因此采用rediss实现tomcat的集群效果.基于redis能够异步讲缓存内容固化到磁盘上,从而当服务器意外重启后,仍然能够让sess ...

  3. SpringBoot,Security4, redis共享session,分布式SESSION并发控制,同账号只能登录一次

    由于集成了spring session ,redis 共享session,导致SpringSecurity单节点的session并发控制失效, springSession 号称 无缝整合httpses ...

  4. Tomcat7.0.99集群使用Redis共享session方案

    以前配置过给予多播的session共享方案,这回再配置一个redis共享session的. 先小小的炫耀一下: 相信大家要做Tomcat+Redis+session配置,遇到的头号麻烦就是编译的tom ...

  5. Spring Boot 使用 Redis 共享 Session 代码示例

    参考资料 博客:spring boot + redis 实现session共享 1. 新建 Maven 工程 我新建 spring-boot-session-redis maven 工程 2. 引入 ...

  6. SpringBoot系列: Redis 共享Session

    Web项目Session管理是一个很重要的话题, 涉及到系统横向扩展, SpringBoot已经为共享Session很好的解决方案, 这篇文章关注使用Redis共享会话, 同时这也是最常用的方法. = ...

  7. ASP.NET MVC 使用Redis共享Session

    储存模式 1.InProc模式 这是ASP.NET默认的Session管理模式,在应用进程内维护Session. 2.StateServer模式 这是在服务器装了.NET环境后自带的一个StateSe ...

  8. 【荐】PHP Session和Cookie,Session阻塞,Session垃圾回收,Redis共享Session,不推荐Memcached保存Session

    什么是 Session 在 web 应用开发中,Session 被称为会话.主要被用于保存某个访问者的数据. 由于 HTTP 无状态的特点,服务端是不会记住客户端的,对服务端来说,每一个请求都是全新的 ...

  9. 一篇文章让你深透理解cookie和session,附带分布式WEB系统redis共享session方案

    cookie和session有什么区别?这是一个很基础的知识点,大家可能都知道一个大概:cookie是存在客户端的,session是存储在服务端,cookie和session用来验证识别用户的登录状态 ...

随机推荐

  1. 简单MVC实现增删改查

    反射工具类RelfectionUtils package Utils; import java.lang.reflect.Field; import java.lang.reflect.Invocat ...

  2. Docker Swarm集群部署

    一.系统环境 1)服务器环境 节点名称 IP 操作系统 内核版本 manager 172.16.60.95 CentOs7 4.16.1-1.el7.elrepo.x86_64 node-01 172 ...

  3. 2.2.1synchronized方法的弊端

    缺陷:用关键字synchronized声明方法是有弊端的,譬如A线程调用同步方法执行一个长时间的任务,那么B线程则必须等待较长的时间, 解决方法:使用synchronized同步语句块 package ...

  4. _ZNote_Qt_对话框_模态非模态

    QDialog(及其子类,以及所有Qt::Dialog类型的类)的对于其 parent 指针都有额外的解释:如果 parent 为 NULL,则该对话框会作为一个顶层窗口,否则则作为其父组件的子对话框 ...

  5. POJ2429--GCD & LCM Inverse (UNSOLVED)

    Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and t ...

  6. 关于QT建立项目中遇到的相关问题的处理办法

    关于QT建立项目中遇到的相关问题的处理办法 1.在QTCreater中新建一个空的工程时,第一次运行会抱一个错误,如下: -1: error: LNK1123: 转换到 COFF 期间失败: 文件无效 ...

  7. 分频器的verilog设计

    笔者最近由于实验室老师的任务安排重新又看了一下分频器的verilog实现,现总结如下,待以后查看之用(重点是查看计数器计到哪个值clk_out进行状态翻转) 1.偶数分频占空比为50% 其实质还是一个 ...

  8. Handle( )

    Handle( ) //得到PB窗口型对象的句柄 功  能:得到PB窗口型对象的句柄.使用该函数可以得到应用对象.窗口或控件的句柄,但不能得到绘图对象的句柄. 语  法:Handle ( object ...

  9. PCB中实现元器件旋转一个角度放置

    我们常常放置器件都是横着或者竖着的...但是有时候需要器件能旋转一个角度放更方便的话,可以这样 设置器件的属性.....

  10. 关于getProperties的一点记录

    写了一很简单的获取配置文件的代码,结果怎么都在报空指针,经过上网查,直到要这样写才不会报: InputStream is = getClass().getClassLoader().getResour ...