方法过滤器,分布式缓存 Memcached实现Session解决方案

控制器-〉方法过滤器-〉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 < 1)
                   {
                       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中获取用户的信息

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

原文地址:https://www.cnblogs.com/ecollab/p/6156398.html