1、多站點共享用戶信息解決方案:javascript
採用分佈式緩存Memcache模擬Session進行用戶信息信息共享css
一、視圖部分html
@{ Layout = null; } <!DOCTYPE html> <html> <head> <title>XX商城後臺管理系統登陸</title> <script type="text/javascript"> if (window.parent.window != window) { window.top.location.href = "/Home/CheckLogin"; } </script> <script src="~/Scripts/jquery-1.8.2.min.js"></script> <script src="~/Scripts/jquery.validate.min.js"></script> <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script> <script type="text/javascript"> function changeCheckCode() { $("#img").attr("src", $("#img").attr("src") + 1); } function afterLogin(data) { if (data=="ok") { window.location.href = "/Home/Index"; } else { $("#errorMsg").text(data); changeCheckCode(); } } </script> <style type="text/css"> * { padding: 0; margin: 0; } body { text-align: center; background: #4974A4; } #login { width: 740px; margin: 0 auto; font-size: 12px; } #loginlogo { width: 700px; height: 100px; overflow: hidden; background: url('/Content/Images/login/logo.png') no-repeat; margin-top: 50px; } #loginpanel { width: 729px; position: relative; height: 300px; } .panel-h { width: 729px; height: 20px; background: url('/Content/Images/login/panel-h.gif') no-repeat; position: absolute; top: 0px; left: 0px; z-index: 3; } .panel-f { width: 729px; height: 13px; background: url('/Content/Images/login/panel-f.gif') no-repeat; position: absolute; bottom: 0px; left: 0px; z-index: 3; } .panel-c { z-index: 2; background: url('/Content/Images/login/panel-c.gif') repeat-y; width: 729px; height: 300px; } .panel-c-l { position: absolute; left: 60px; top: 40px; } .panel-c-r { position: absolute; right: 20px; top: 50px; width: 222px; line-height: 200%; text-align: left; } .panel-c-l h3 { color: #556A85; margin-bottom: 10px; } .panel-c-l td { padding: 7px; } .login-text { height: 24px; left: 24px; border: 1px solid #e9e9e9; background: #f9f9f9; } .login-text-focus { border: 1px solid #E6BF73; } .login-btn { width: 114px; height: 29px; color: #E9FFFF; line-height: 29px; background: url('/Content/Images/login/login-btn.gif') no-repeat; border: none; overflow: hidden; cursor: pointer; } #txtUsername, #code, #txtPassword { width: 191px; } #logincopyright { text-align: center; color: White; margin-top: 50px; } a { color: Black; } a:hover { color: Red; text-decoration: underline; } </style> </head> <body style="padding: 10px"> <div id="login"> <div id="loginlogo"> </div> <div id="loginpanel"> <div class="panel-h"> </div> <div class="panel-c"> <div class="panel-c-l"> @using (Ajax.BeginForm("CheckLogin", new { }, new AjaxOptions() { OnSuccess = "afterLogin" }, new { id = "loginForm" })) { <table cellpadding="0" cellspacing="0"> <tbody> <tr> <td align="left" colspan="2"> <h3> 請使用OA系統帳號登陸 </h3> </td> </tr> <tr> <td align="right"> 帳號: </td> <td align="left"> <input type="text" name="LoginCode" id="LoginCode" class="login-text" /> </td> </tr> <tr> <td align="right"> 密碼: </td> <td align="left"> <input type="password" name="LoginPwd" id="LoginPwd" value="123" class="login-text" /> </td> </tr> <tr> <td> 驗證碼: </td> <td align="left"> <input type="text" class="login-text" id="code" name="vCode" value="1" /> </td> </tr> <tr> <td></td> <td> <img id="img" src="/Login/ValidateCode/?id=1" style="float: left; height: 24px;" /> <div style="float: left; margin-left: 5px; margin-top: 10px;"> <a href="javascript:void(0)" onclick="changeCheckCode();return false;">看不清,換一張</a> </div> </td> </tr> <tr> <td align="center" colspan="2"> <input type="submit" id="btnLogin" value="登陸" class="login-btn" /><a href="/Login/FindPwd">找回密碼</a> <input type="checkbox" name="checkMe" value="1" />記住我 <span id="errorMsg"></span> </td> </tr> </tbody> </table> } </div> <div class="panel-c-r"> <p> 請從左側輸入登陸帳號和密碼登陸 </p> <p> 若是遇到系統問題,請聯繫網絡管理員。 </p> <p> 若是沒有帳號,請聯繫網站管理員。 </p> <p> ...... </p> </div> </div> <div class="panel-f"> </div> </div> <div id="logincopyright"> Copyright ? 2012 Yilian.com </div> </div> </body> </html>
登陸頁面展現:java
二、控制器部分jquery
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 } }
2、訪問頁面先驗證用戶是否登陸的解決辦法:ajax
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; } /// <summary> /// 執行控制器方法以前先執行該方法 /// 獲取自定義SessionId的值,而後從Memcache中取出 /// </summary> /// <param name="filterContext"></param> 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<UserInfo>(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(); } } }
3、源碼下載:緩存
點擊下載源碼>>網絡