储存模式

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(-24);
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

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

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

ASP.NET MVC 使用Redis共享Session的更多相关文章

  1. 使用 Redis 共享 Session 会话

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

  2. 如何在C#Asp.Net MVC使用Redis缓存

    为什么要在Asp.Net MVC项目中使用Redis缓存呢?系统是按照高负载高并发来设计的,这就涉及服务器集群带来的问题,Session存储验证码或登录信息,在系统登录的时候,可能展示登录界面和存储验 ...

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

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

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

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

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

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

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

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

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

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

  8. Asp.net mvc与PHP的Session共享的实现

    最近在做的一个ASP.NET MVC的项目,要用到第三方的php系统,为了实现两个系统的互联互通.决定将两者的session打通共享.让asp.net mvc 和php 都能正常访问和修改Sessio ...

  9. ASP.NET下跨应用共享Session和使用Redis进行Session托管简介

    在之前的博客中,我说到了Session的共享问题,其中说到了Web Farm和Web Garden两种情况下Session的处理.在ASP.NET提供的Session处理方法中,有以下四种模式: 1. ...

随机推荐

  1. C++ 著名程序库 概览

          本文转载自: http://ace.acejoy.com/thread-3777-1-1.html   1.C++各大有名库的介绍--C++标准库 2.C++各大有名库的介绍--准标准库B ...

  2. proto序列化

    proto序列化和反序列化类 序列化是将我们的Protobuf类对象转换成字节数组的方法,网络底层我们从服务器获取到的是字节数组,反序列化是将字节转成proto类对象的方法 public class ...

  3. 检出商品详情中的图片并替换url

    原有的批量导入是按照系统本身的功能导入商品,现在需要用接口将图片上传图片服务器 所以需要将批量导入的商品图片取出来,上传后替换掉原来的url (1)检出详情中的图片,用文件名做key private ...

  4. CorelDRAW中六种复制对象的方法详解

    复制可保证对象的大小一致,复制也是所有操作中最基本的操作.CorelDRAW软件中支持多种复制对象的操作,本教程将详解CorelDRAW中六种复制对象的方法. 方法一 选择复制对象,点击编辑→复制,再 ...

  5. 海康视频监控---Demo

    1,使用在页面中调用ActiveX控件 <object classid='clsid:E7EF736D-B4E6-4A5A-BA94-732D71107808' codebase='' stan ...

  6. ios开发之--使用UILabel Category 计算UILabel内容大小

    在此仅做记录,代码如下:

  7. 中间件安全加固之Jboss

    JBoss 的安全设置 1) jmx-console A.jmx-console登录的用户名和密码设置 默认情况访问 http://localhost:8080/jmx-console 就可以浏览jb ...

  8. Window关闭端口的方法(445/135/137/138/139/3389等)

    为防止漏洞被利用,需要采取必要措施,关闭以上端口,以保证系统更加安全. window2003 关闭135端口的方法 要关闭此端口,只需停止DCOM接口服务即达到目的.下面是详细操作过程. 1.打开“组 ...

  9. informix中的时间计算

    今天看SUN服务器是的mail(vi   /var/mail/xxxuser),发现定时任务上的一些存储过程执行有错误,其中有一个错误是long transaction,长事务错误,到数据库一查,天哪 ...

  10. 第7章 Ajax数据爬取

    Ajax 简介 Ajax 分析方法 Ajax 结果提取