一、多站点共享用户信息解决方案:
采用分布式缓存Memcache模拟Session进行用户信息信息共享
1、视图部分
@{ Layout = null;}XX商城后台管理系统登录
登录页面展示:
2、控制器部分
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace WebApp.Controllers{ public class LoginController : Controller { // GET: /Login/ CZBK.HeiMaOA.IBLL.IUserInfoService userInfoService { get; set; } public ActionResult Index() { return View(); } #region 用户登录 public ActionResult CheckLogin() { string validateCode = Session["validateCode"] == null ? string.Empty : Session["validateCode"].ToString(); if (string.IsNullOrEmpty(validateCode)) { return Content("验证码错误!"); } //清空防止暴力破解 Session["validateCode"] = null; string requestCode = Request["vCode"]; if (!requestCode.Equals(validateCode, StringComparison.InvariantCultureIgnoreCase)) { return Content("验证码错误!"); } string userName = Request["LoginCode"]; string userPwd = Request["LoginPwd"]; //对用户名、密码进行过滤 var userInfo = userInfoService.LoadEntities(u => u.UName == userName && u.UPwd == userPwd).FirstOrDefault(); if (userInfo == null) { return Content("用户名密码错误!"); } else { //Session["userInfo"] = userInfo; //普通方式 #region 利用Memcache模拟Session进行共享用户Session信息 //自己创建的SessionId,作为Memcache的Key string sessionId = Guid.NewGuid().ToString(); //将用户的信息存储到Memcache中 CZBK.HeiMaOA.Common.MemcacheHelper.Set(sessionId, CZBK.HeiMaOA.Common.SerializerHelper.SerializerToString(userInfo)); //然后将自创的SessionId以Cookie的形式返回给浏览器,存储到浏览器端的内存中。 Response.Cookies["sessionId"].Value = sessionId; #endregion return Content("ok"); } } #endregion #region 展示验证码 public ActionResult ValidateCode() { CZBK.HeiMaOA.Common.ValidateCode validateCode = new CZBK.HeiMaOA.Common.ValidateCode(); string code = validateCode.CreateValidateCode(4); Session["validateCode"] = code; byte[] buffer = validateCode.CreateValidateGraphic(code); return File(buffer, "image/jpeg"); } #endregion }}
二、访问页面先验证用户是否登录的解决办法:
1.新建BaseController,让需要验证的继承这个控制器即可:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using CZBK.HeiMaOA.Model;namespace WebApp.Controllers{ public class BaseController : Controller { public UserInfo LoginUser { get; set; } ////// 执行控制器方法之前先执行该方法 /// 获取自定义SessionId的值,然后从Memcache中取出 /// /// protected override void OnActionExecuting(ActionExecutingContext filterContext) { bool isExt = false; if (Request.Cookies["sessionId"] != null) { //获取自定义的SessionId string sessionId = Request.Cookies["sessionId"].Value; object obj = CZBK.HeiMaOA.Common.MemcacheHelper.Get(sessionId); if (obj != null) { LoginUser = CZBK.HeiMaOA.Common.SerializerHelper.DeserializeToObject(obj.ToString()); isExt = true; } } if (!isExt) //用户没登录 { filterContext.HttpContext.Response.Redirect("/Login/Index"); } base.OnActionExecuting(filterContext); } }}
2.示例:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace WebApp.Controllers{ public class HomeController : BaseController { // // GET: /Home/ public ActionResult Index() { if (LoginUser != null) { ViewData["userName"] = LoginUser.UName; } return View(); } }}
三、源码下载: