Memcache+Cookie替代Session解决方案(MVC版)
阅读目录
通过IHttpModule注册过滤管道方式
CheckLoginModule.cs文件内容如下:
using System;
using System.Web;
using MvcMamcache.Models; namespace MvcMamcache.Common {
public class CheckLoginModule : IHttpModule {
#region IHttpModule 成员 public void Init(HttpApplication context) {
context.AcquireRequestState += new EventHandler(OnRequest);
} public void OnRequest(object source, EventArgs e) {
HttpApplication application = source as HttpApplication;//得到Application
HttpContext context = application.Context;//得到请求上下文.
Uri url = context.Request.Url;//得到当前请求的URL if(url.AbsolutePath.ToLower() == "/home/index") {
var httpCookie = context.Request.Cookies["SessionId"];
if(httpCookie == null || Common.MemCacheHelper.GetValue(httpCookie.Value) == null) {//Session会话过期
//是否保留7天
if(context.Request.Cookies["ui1"] == null || context.Request.Cookies["ui2"] == null) {
context.Response.Redirect("/home/login");
} else {
//根据Cookie中保存的用户名与密码至数据库获取UserInfo对象 这里直接new
UserInfo userInfo = new UserInfo() {
LoginId = context.Request.Cookies["ui1"].Value,
LoginPwd = context.Request.Cookies["ui2"].Value
};
var sessionId = Guid.NewGuid().ToString();
context.Response.Cookies["SessionId"].Value = sessionId;
Common.MemCacheHelper.Insert(sessionId, userInfo);
}
}
}
} public void Dispose() {
throw new NotImplementedException();
}
#endregion
}
}
需要将此过滤Module在Web.Config文件中进行配置
<httpModules>
<add name="CheckLoginModule"
type="MvcMamcache.Common.CheckLoginModule"/>
</httpModules>
Home控制器中Index与Login方法的调整
通过BaseController
自定义的父类控制器 它继承自Controller 在控制器Initialize中 进行校验 并声明LoginUserInfo属性保存用户信息供所有子Controller使用
using System;
using System.Web.Mvc;
using MvcMamcache.Models; namespace MvcMamcache.Common {
public class BaseController : Controller {
protected UserInfo LoginUserInfo {
get;
set;
} protected override void Initialize(System.Web.Routing.RequestContext requestContext) {
base.Initialize(requestContext);
if(requestContext.HttpContext.Request.Cookies["SessionId"] != null) {
string guid = requestContext.HttpContext.Request.Cookies["SessionId"].Value;
object obj = Common.MemCacheHelper.GetValue(guid);//根据Cookie的值从缓存中取出数据.
if(obj != null) {
UserInfo model = obj as UserInfo;
LoginUserInfo = model;
} else {
CheckCookieSevenDay(requestContext);//7天Cookie检验
}
} else {
Response.Redirect("/home/login");
}
} private void CheckCookieSevenDay(System.Web.Routing.RequestContext requestContext) {
var context = requestContext.HttpContext;
if(context.Request.Cookies["ui1"] == null || context.Request.Cookies["ui2"] == null) {
context.Response.Redirect("/home/login");
} else {
//根据Cookie中保存的用户名与密码至数据库获取UserInfo对象 这里直接new
UserInfo userInfo = new UserInfo() {
LoginId = context.Request.Cookies["ui1"].Value,
LoginPwd = context.Request.Cookies["ui2"].Value
};
var sessionId = Guid.NewGuid().ToString();
context.Response.Cookies["SessionId"].Value = sessionId;
Common.MemCacheHelper.Insert(sessionId, userInfo);
}
}
}
}
需要用户状态校验的控制器中
using System.Web.Mvc;
using MvcMamcache.Common; namespace MvcMamcache.Controllers {
public class AdminController : BaseController {
//
// GET: /Admin/ public ActionResult Index() {
ViewBag.UserName = LoginUserInfo.LoginId;
return View();
} }
}
关于滑动过期
f(Common.MemCacheHelper.GetValue("GetNow") == null) {
Common.MemCacheHelper.Insert("GetNow", DateTime.Now, DateTime.Now.AddMinutes());
ViewBag.Msg = "新增时间" + Common.MemCacheHelper.GetValue("GetNow").ToString() + "失效时间应该在:" + ((DateTime)Common.MemCacheHelper.GetValue("GetNow")).AddMinutes();
} else {
Common.MemCacheHelper.Replace("GetNow", DateTime.Now, DateTime.Now.AddMinutes());
ViewBag.Msg = "替换时间" + Common.MemCacheHelper.GetValue("GetNow").ToString() + "失效时间应该在:" + ((DateTime)Common.MemCacheHelper.GetValue("GetNow")).AddMinutes();
当然session的替代方案远不止使用Memcached 还可以通过Nosql非关系型数据库,话说ManagDB的效率相当称赞
Memcache+Cookie替代Session解决方案(MVC版)的更多相关文章
- Memcache+cookie实现模拟session
上一片讲到Memcached在Windows上的安装,和用Telnet工具进行命令操作,在稍微了解了原理之后,我也就开始尝试着用程序来对Memcached进行操作.这一篇分为两个部分,第一部分是用.n ...
- 10.Django基础八之cookie和session
一 会话跟踪 我们需要先了解一下什么是会话!可以把会话理解为客户端与服务器之间的一次会晤,在一次会晤中可能会包含多次请求和响应.例如你给10086打个电话,你就是客户端,而10086服务人员就是服务器 ...
- 【转】Cookie和Session区别和联系详解
会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...
- 理解Cookie和Session机制(转)
目录[-] Cookie机制 什么是Cookie 记录用户访问次数 Cookie的不可跨域名性 Unicode编码:保存中文 BASE64编码:保存二进制图片 设置Cookie的所有属性 Cookie ...
- 关于cookie的文章(cookie与session机制)
会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...
- 理解Cookie和Session机制
转载: 理解Cookie和Session机制 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录 ...
- cookie 和 session
会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...
- 程序中的Cookie 和Session
这几天回家休息后,想想放假之前的几天,主要看的一些工作上的东西,发现对Session和Cookie这两个东西,我还是很陌生.恩,趁着有网,看了点相关的资料,打算整理下.一翻博客,发现已经有前辈已经对这 ...
- ASP.NET Cookie和Session
Cookie和Session C#在服务器,JS在客户端 客户端验证不能代替服务端验证 Http HTTP属于应用层,HTTP 协议一共有五大特点:1.支持客户/服务器模式;2.简单快速;3.灵活;4 ...
随机推荐
- quartz报错 Couldn't retrieve job because the BLOB couldn't be deserialized: null
今天线上添加定时任务之后 定时任务查询页面报出如上错误, 原因有两点 1.org.quartz.jobStore.useProperties = true 这个属性的意思存储的JobDataMaps是 ...
- C#Remoting
C# Remoting 细细品味C#——.Net Remoting专题 http://www.cnblogs.com/xia520pi/archive/2011/11/02/2233371.htm ...
- 24_java之转换流和缓冲流
01转换流概述 * A: 转换流概述 * a: 转换流概述 * OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的字符编码表,将要写入流中的字符编码成字节 * 将字符串按照指 ...
- Django-2的路由层(URLconf)
URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于客户端发来的某个URL调用哪一段逻辑代码 ...
- MySQL Root密码丢失解决方法总结
1. 检查my.cnf,看看有没有密码......靠这也算一条啊 2. 如果能够重启,首先使用–skip-grant-tables参数重启,然后改密码,再去掉–skip-grant-tables参数重 ...
- 使用Eclipse搭建JavaWeb开发环境的几个基本问题
Eclipse搭建JavaWeb开发环境 eclipse是一个用于java程序开发的ide软件,tomcat是一个运行javaweb应用的服务器软件,使用eclipse开发javaweb应用的时,首要 ...
- Apache Hive (七)Hive的DDL操作
转自:https://www.cnblogs.com/qingyunzong/p/8723271.html 库操作 1.创建库 语法结构 CREATE (DATABASE|SCHEMA) [IF NO ...
- 【总结整理】UGC内容
除了内容了产品,还有什么适合引入UGC? :引发讨论,诱导参与,然后促成销售. User Generated Content,也就是用户生成内容的意思. 购买类产品,内容催生购买 1.为用户购买提供思 ...
- MobileMovieTexture播放视频
MobileMovieTexture插件支持IOS系统播放视频文件.简单,方便
- 523. Continuous Subarray Sum是否有连续和是某数的几倍
[抄题]: Given a list of non-negative numbers and a target integer k, write a function to check if the ...