储存模式

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. goole Advance client 离线安装

    1.下载插件:Advanced Rest Client 2.最新版的Chrome不支持本地安装插件,所以我们要使能开发者模式 3.把插件后缀名crx改为zip 4.解压,点击‘加载正在开发的扩展程序’ ...

  2. Keras分类问题

    #-*- coding: utf-8 -*- #使用神经网络算法预测销量高低 import pandas as pd #参数初始化 inputfile = 'data/sales_data.xls' ...

  3. AngularJS实战之路由ui-view传参

    angular路由传参 首页 <!DOCTYPE html> <html ng-app="app"> <head> <title>路 ...

  4. Linux(CentOS)下的apache服务器配置与管理

    原文链接:http://blog.csdn.net/ylqmf/article/details/5291680 一.WEB服务器与Apache1.web服务器与网址 2.Apache的历史 3.补充h ...

  5. 11-border(边框)

    边框 border:边框的意思,描述盒子的边框 边框有三个要素: 粗细 线性样式 颜色 border: solid 如果颜色不写,默认是黑色.如果粗细不写,不显示边框.如果只写线性样式,默认的有上下左 ...

  6. Linux安装Oracle 11g Grid Infrastructure 出现OUI-10182错误解决办法

      已确保安装的ORACLE_BASE目录是属于grid:oinstall 但安装时总是报:OUI-10182 The effective user ID does not match the own ...

  7. 常用模块xml,shelve,configparser,hashlib

    XML 什么XML:全称 可扩展标记语言 标记指的是代表某种含义的字符 XML<> 为什么需要XML 为能够在不同的平台间继续数据的交换 为了使交换的数据能让对方看懂 就需要按照一定的语法 ...

  8. HTML5表单新属性

    HTML5表单新属性 autofocus  自动聚焦 <input type="text" autofocus> placeholder占位文本 tel         ...

  9. [转]Windows中使用命令行方式编译打包Android项目

    http://my.oschina.net/liux/blog/37875 网上很多用Ant来编译打包Android应用的文章,毕竟Ant是纯Java语言编写的,具有很好的跨平台性.今天想写个纯win ...

  10. UIImageView的frame设置(4种方式)

    查看图片宽高后,再去设置UIImageView的frame.(不推荐) 用代码自动获取图片的宽高来设置UIImageView的frame: 创建UIImageView的同时,直接设置UIImageVi ...