session保存至缓存中

原理

session实际上是把session 保存在了cookie中,用户每次访问都发送sessionid给服务器,服务器根据sessionid取用户数据

 

1把session保存到缓存服务器中 

namespace XFT.Service
{
    /// <summary>
    ///用户状态管理
    /// </summary>
    public class Session
    {
        private HttpContextBase context;

        public const string SessionName = "SessionId_";

        /// <summary>
        /// 当前SessionId.
        /// </summary>
        /// <value>The session identifier.</value>
        public string SessionId { get; set; }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="_context"></param>
        public Session(HttpContextBase _context)
        {
            var context = _context;
            var cookie = context.Request.Cookies.Get(SessionName);
            if (cookie == null || string.IsNullOrEmpty(cookie.Value))
            {
                SessionId = SessionName + Guid.NewGuid();
                context.Response.Cookies.Add(new HttpCookie(SessionName, SessionId));
                context.Request.Cookies.Add(new HttpCookie(SessionName, SessionId));
            }
            else
            {
                SessionId = cookie.Value;
            }
        }

        /// <summary>
        /// 获取当前用户信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns>System.Object.</returns>
        public object Get<T>() where T : class,new()
        {
            return xftwl.Infrastructure.CouchBaseHelper.DocumentGet<T>(SessionId);
        }

        /// <summary>
        /// 用户是否在线
        /// </summary>
        /// <returns><c>true</c> if this instance is login; otherwise, <c>false</c>.</returns>
        public bool IsLogin()
        {
            return xftwl.Infrastructure.CouchBaseHelper.DocumentExist(this.SessionId);
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj">The object.</param>
        public void Login<T>(T obj) where T : class,new()
        {
            xftwl.Infrastructure.CouchBaseHelper.DocumentUpsert<T>(SessionId, obj, 60 * 30);
        }

        /// <summary>
        /// 退出
        /// </summary>
        public void Logout()
        {
            xftwl.Infrastructure.CouchBaseHelper.DocumentRemove(SessionId);
        }

        /// <summary>
        /// 续期
        /// </summary>
        public void Postpone()
        {
            if (xftwl.Infrastructure.CouchBaseHelper.DocumentExist(SessionId))
            {
                var user = xftwl.Infrastructure.CouchBaseHelper.DocumentGet<Data.LoginUserModel>(SessionId);
                if (user != null)
                {
                    xftwl.Infrastructure.CouchBaseHelper.DocumentUpsert<Data.LoginUserModel>(SessionId, user, 60 * 30);
                }
            }

        }

        /// <summary>
        /// 在线用户数量
        /// </summary>
        /// <returns>System.Int32.</returns>
        public int Count()
        {
            return 10000;
        }
    }
}
保存session会话至缓存

 

2在asp.net mvc项目中创建过滤器,保证每次访问时session都自动续期

using XFT.Service;
namespace Senparc.Weixin.MP.Sample
{
    public class SessionFilterAttribute:ActionFilterAttribute
    {
        /// <summary>
        /// 每次请求都续期
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            new Session(filterContext.HttpContext).Postpone();
        }
    }
}
自定义过滤器

 

3在asp.net mvc项目 创建类SessionExt.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


/// <summary>
/// session扩展信息
/// </summary>
public static class SessionExt
{
    public static XFT.Service.Session SessionInfo(this HttpContextBase context)
    {
        return new XFT.Service.Session(context);
    }

    public static XFT.Service.Session SessionInfo(this Controller controller)
    {
        return new XFT.Service.Session(controller.HttpContext);
    }
}
session扩展

 

 4 使用

using System.Web.Mvc;

namespace Senparc.Weixin.MP.Sample.Controllers
{
    public class AccountController : Controller
    {
        public ActionResult Index()
        {
            //获取登录用户信息
            var userinfo = this.SessionInfo().Get<XFT.Data.LoginUserModel>();
            return View();
        }
        public ActionResult Login(string name)
        { 
            var userinfo=new XFT.Data.LoginUserModel { LoginName=name };
            //验证后设置为登录状态
            this.SessionInfo().Login<XFT.Data.LoginUserModel>(userinfo);
        }
   
调用session会话的例子

 

posted @ 2016-05-03 18:48  奎宇工作室  阅读(909)  评论(0编辑  收藏  举报