储存模式

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. 如何悄悄地提升MySQL用户权限

    温馨提示: 一次成功的非法提权,需要的必备条件是:1.对mysql权限表的查.改权限: 2.一次不经意的数据库服务器重启: 此次测试版本:5.6.25 准备邪恶用户: grant update on ...

  2. 工作总结(二):Web Design

    PHP框架:CakePHP 前端框架:Bootstrap Payment Data Transfer:https://developer.paypal.com/docs/classic/paypal- ...

  3. file新建文件及文件夹

    1.获取包名的根目录:mRootPath = getFilesDir().getParent(); // ====mRootPath===/data/data/com.yoyu.file  获取SD卡 ...

  4. django天天生鲜项目

    .后台admin管理天天生鲜商品信息 models里 from django.db import modelsfrom tinymce.models import HTMLField #需要pip安装 ...

  5. ASP.NET MVC Owin 基本理解

    一.OWIN OWIN(Open Web Interface for .Net),定义了一个服务器(IIS)和Web应用程序(MVC,Webform)通信的标准接口,并且通过抽象层使得这两个在微软平台 ...

  6. day03_雷神_文件操作

    day03 上周回顾_问题总结: 地址值: li = [] name = ['name','price','count'] dic = {} #如果这里定义空列表,后边的dic[name[i]] = ...

  7. 取得cxgrid的表格的值,仔细看下面的代码

    procedure TfrmMain.cxGridDBTableView_List_PSSJCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas ...

  8. Linux-文件查找命令find

    find - search for files in a directory hierarchy find命令用于在目录层级中查找文件 SYNOPSIS find [-H] [-L] [-P] [-D ...

  9. 我最常用的7个Web在线工具

    为什么要用Web在线工具呢?有两个原因,第一,它不受限于物理平台,我既可以在自己的电脑上使用,也可以在公司或亲戚朋友的电脑上使用(不管对方的操作系统是什么,只要能上网):第二,可以解放硬盘,减少PC端 ...

  10. JS学习笔记7_表单脚本

    1.获取表单及表单元素引用的方式 var mForm = document.forms[formName];获取表单引用 mForm.elements[elemName]获取表单元素,如有同名的,则得 ...