控制器-〉方法过滤器-〉controller-> 方法

所以通过建立controller基类的方法进行方法过滤,所有控制器先执行基类的OnActionExecuting 方法。

using Spring.Context;
using Spring.Context.Support;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace Eco.Web.App.Controllers
{
public class BaseController : Controller
{
//
// GET: /Base/
public UserInfo LoginUser { get; set; }
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
bool isExt = false;
// if (Session["userInfo"] == null)
if (Request.Cookies["sessionId"]!=null)
{
string sessionId = Request.Cookies["sessionId"].Value;//接收从Cookie中传递过来的Memcache的key
object obj= Common.MemcacheHelper.Get(sessionId);//根据key从Memcache中获取用户的信息
if (obj != null)
{
UserInfo userInfo = Common.SerializerHelper.DeserializeToObject<UserInfo>(obj.ToString());
LoginUser = userInfo;
isExt = true;
//Common.MemcacheHelper.Set(sessionId, obj.ToString(), DateTime.Now.AddMinutes(20));//模拟滑动过期时间
if (LoginUser.UName == "itcast")
{
return;
} //完成权限过滤.
string actionUrl = Request.Url.AbsolutePath.ToLower();//请求地址。
string actionHttpMethod = Request.HttpMethod;//请求方式
IApplicationContext ctx = ContextRegistry.GetContext();
IUserInfoService UserInfoService = (IUserInfoService)ctx.GetObject("UserInfoService");
IActionInfoService ActionInfoService = (IActionInfoService)ctx.GetObject("ActionInfoService");
var actionInfo= ActionInfoService.LoadEntities(a=>a.Url==actionUrl&&a.HttpMethod==actionHttpMethod).FirstOrDefault();
if (actionInfo == null)
{
Response.Redirect("/Error.html");
return;
} //判断登录用是否有权限访问
//按照第2条进行判断
var loginUserInfo = UserInfoService.LoadEntities(u => u.ID == LoginUser.ID).FirstOrDefault();
var r_UserInfo_actionInfo =( from a in loginUserInfo.R_UserInfo_ActionInfo
where a.ActionInfoID == actionInfo.ID
select a).FirstOrDefault();
if (r_UserInfo_actionInfo != null)
{
if (r_UserInfo_actionInfo.IsPass == true)
{
return;
}
else
{
Response.Redirect("/Error.html");
return;
}
}
//按照第1条线进行过滤(用户---角色--权限)
var loginUserRoleInfo = loginUserInfo.RoleInfo;
var loginUserCountAction=(from r in loginUserRoleInfo
from a in r.ActionInfo
where a.ID==actionInfo.ID
select a
).Count();
if (loginUserCountAction < )
{
Response.Redirect("/Error.html");
return;
} } }
if (!isExt)
{
filterContext.HttpContext.Response.Redirect("/Login/Index");
return;
} } }
}

controller基类

http://www.cnblogs.com/jaxu/p/5196811.html  memcache  http://www.cnblogs.com/knowledgesea/p/4940713.html

客户端添加

Commons.dll、ICSharpCode.SharpZipLib.dll、Memcached.ClientLibrary.dll引用

建立memcacheHelper辅助类

将地址写到Web.config文件中

string[] serverlist = { "127.0.0.1:11211", "10.0.0.132:11211" };

建立序列化(JSON)SerializerHelper类,缓存对象.用json.net开源项目序列化对象  VS已经集成到MVC中,不用在MVC项目中引入包,但在非MVC项目中需要引入包(在MVC packages中找),如COMMON项目中要引入

通过向Cookies存入sessionId 来存、取session,只要浏览器不关闭就可以取出

string sessionId =Guid.NewGuid().ToString();//作为Memcache的key
Common.MemcacheHelper.Set(sessionId,Common.SerializerHelper.SerializeToString(userInfo), DateTime.Now.AddMinutes(20));//使用Memcache代替Session解决数据在不同Web服务器之间共享的问题。
Response.Cookies["sessionId"].Value = sessionId;//将Memcache的key以cookie的形式返回到浏览器端的内存中,当用户再次请求其它的页面请求报文中会以Cookie将该值再次发送服务端。

....................

if (Request.Cookies["sessionId"]!=null)
{
string sessionId = Request.Cookies["sessionId"].Value;//接收从Cookie中传递过来的Memcache的key
object obj= Common.MemcacheHelper.Get(sessionId);//根据key从Memcache中获取用户的信息

-------------------------------------------------------------------------------------------------------------------------------------

方法过滤器,分布式缓存 Memcached实现Session解决方案的更多相关文章

  1. CYQ.Data V5 分布式缓存MemCached应用开发介绍

    前言 今天大伙还在热议关于.NET Core的东西,我只想说一句:在.NET 跨平台叫了这么多年间,其实人们期待的是一个知名的跨平台案例,而不是一堆能跨平台的消息. 好,回头说说框架: 在框架完成数据 ...

  2. 分布式缓存Memcached/memcached/memcache详解及区别

    先来解释下标题中的三种写法:首字母大写的Memcached,指的是Memcached服务器,就是独立运行Memcached的后台服务器,用于存储缓存数据的“容器”.memcached和memcache ...

  3. .NET分布式缓存Memcached从入门到实战

    一.课程介绍 在数据驱动的web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载.缓存是解决这个问题的好办法.但是ASP.NET中的虽然已经可以实现对页面局部进行缓存,但还是 ...

  4. NET分布式缓存Memcached测试体验

    原文地址:http://onlyonewt.blog.sohu.com/160168896.html 一直在学习关注大访问量网站的缓存是如何实现,之前看过Memcached的资料,忙于没有时间来真正测 ...

  5. 第八章 企业项目开发--分布式缓存memcached

    注意:本节代码基于<第七章 企业项目开发--本地缓存guava cache> 1.本地缓存的问题 本地缓存速度一开始高于分布式缓存,但是随着其缓存数量的增加,所占内存越来越大,系统运行内存 ...

  6. 分布式缓存Memcached

       分布式缓存服务器,既然用到数据缓存很明显就是想高效性的获取数据,大容量的存储数据.为了可以缓存大量的数据以及可以高效获取数据,那么分布式缓存数据库就要解决数据可以水平线性扩展,这样可以扩大数据容 ...

  7. 分布式缓存-Memcached

    分布式缓存出于如下考虑,首先是缓存本身的水平线性扩展问题,其次是缓存大 并发下的本身的性能问题,再次避免缓存的单点故障问题(多副本和副本一致性).分布式缓存的核心技术包括首先是内存本身的管理问题,包括 ...

  8. C# Azure 存储-分布式缓存Redis在session中的配置

    1. 开始 对于分布式的缓存,平常的session的处理是一个用户对应一台分布式的机器,如果这台机器中途挂机或者不能处理这个用户session的情况发生,则此用户的session会丢失,会发生不可预知 ...

  9. 分布式缓存memcached介绍,win7环境安装,常用命令set,get,delete,stats, java访问

    一.memcached是什么? 二.memcached不互相通信的分布式 三.安装步骤 四.本文介绍的命令主要包括: 存入命令(Storage commands) 取回命令(Retrieval com ...

随机推荐

  1. Oracle 部分函数使用说明

    oracle有些函数可能我知道是什么作用,但是具体其实说不清楚,这里是我这几天看到的函数使用方法及说明,记录一下,以后看看 --1.replace('str',oldVal,newVal)替换功能方法 ...

  2. SBT Assembly - Deduplicate error & Exclude error

    sbt assembly java.lang.RuntimeException: deduplicate: different file contents found in the following ...

  3. wget 下载整个网站,或者特定目录

    需要下载某个目录下面的所有文件.命令如下 wget -c -r -np -k -L -p www.xxx.org/pub/path/ 在下载时.有用到外部域名的图片或连接.如果需要同时下载就要用-H参 ...

  4. asp.net 修改/删除站内目录操作后会导致Session丢失

    http://www.jb51.net/article/21770.htm http://blog.chinaunix.net/uid-7425507-id-134216.html 在Web项目中使用 ...

  5. wxpython更新

    .configure时候检查不到gtk+ 使用 apt-get install gnome-core-devel

  6. FIO 测试磁盘iops 以及读写

    最近在做mariadb的性能,感觉io 有瓶颈,就使用fio 来测试一下磁盘.下文为转载文章(温馨提示:此命令很伤硬盘,测试前请备份数据,- -我就写坏了一个.) FIO 是测试IOPS的非常好的工具 ...

  7. vue 倒计时

    简单粗暴 export default { data () { return { timer: 30, //默认倒数30秒 stop : false, //默认是停止的,但界面加载之后会变成false ...

  8. ping & tracert over TCP

    偶然发现还有这样的工具: 通过TCP协议实现ping和tracert. 之前一直苦恼无法通过ping的方式测试被q网站, 现在有了这两个工具后就方便了. [Windows] tcping: http: ...

  9. YAML 模板文件语法

    YAML 模板文件语法 默认的模板文件是 docker-compose.yml,其中定义的每个服务都必须通过 image 指令指定镜像或 build 指令(需要 Dockerfile)来自动构建. 其 ...

  10. Excel 中 Index 和 Match 方法的使用

    MATCH函数(返回指定内容所在的位置) MATCH(lookup-value,lookup-array,match-type) lookup-value:表示要在区域或数组中查找的值,可以是直接输入 ...