/// <summary> /// 获取当前登录管理员的管理员信息 /// </summary> /// <returns></returns> protected Admin GetAdmin() { HttpCookie recordCookie = Request.Cookies["sh"]; Admin admin = CacheHelper.GetCache(recordCookie.Value) as Admin; if (admin == null) { //从数据库中查询 AdminRecord record = adminRecordApplication.Get(recordCookie.Value); if (record == null) { return(null); } admin = record.Admin; CacheHelper.SetCache(recordCookie.Value, admin); } return(admin); }
public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); if (filterContext.ActionDescriptor.GetCustomAttributes(typeof(NoFilter), false).Length > 0) { //登录页不使用过滤器 //TODO 检测一下有木有登录记录,有登录记录的话直接登录,否则放行 return; } HttpContext context = HttpContext.Current; HttpCookie recordCookie = context.Request.Cookies["sh"];//said history=>Said登录历史 string recordId = recordCookie == null ? null : recordCookie.Value; //System.Diagnostics.Debug.Write(); //从缓存读,读不到再从数据库读 if (string.IsNullOrEmpty(recordId)) { //跳转到登录页 filterContext.Result = new RedirectResult(string.Format("/Back/Home/Login?re={0}", System.Web.HttpUtility.UrlEncode(context.Request.Url.AbsoluteUri))); return; } //没有记录 && 从数据库读不到记录 if (CacheHelper.GetCache(recordId) == null) { AdminRecord record = adminRecordApplication.Get(recordId); if (record != null) { CacheHelper.SetCache(recordId, record.Admin); } else { /* * 跳转到登录页,RedirectResult参考这篇:http://www.cnblogs.com/artech/archive/2012/08/16/action-result-04.html * RedirectResult是暂时重定向,搜索引擎不会收录 */ filterContext.Result = new RedirectResult(string.Format("/Back/Home/Login?re={0}", System.Web.HttpUtility.UrlEncode(context.Request.Url.AbsoluteUri))); } } }