public string GetUserInfo() { IApplicationContext ctx = ContextRegistry.GetContext(); IUserInfoBll userInfoBll = (IUserInfoBll)ctx.GetObject("UserInfoBll"); var userInfo = userInfoBll.LoadEntities(u => u.DelFlag == (short)DeleteEnumType.Normal).ToList(); return(Common.SerializeHelper.SerializeToString(userInfo)); }
// GET: Login public ActionResult Index() { //检查Session是否存在 //if (Session["UserInfo"] == null) if (Request["sessionID"] == null) { //检查Cookie储存的用户信息 if (Request.Cookies["UserName"] != null) { string userName = Request.Cookies["UserName"].Value; //IOC IApplicationContext ctx = ContextRegistry.GetContext(); IUserInfoBll userInfoBll = (IUserInfoBll)ctx.GetObject("UserInfoBll"); UserInfo userInfo = userInfoBll.LoadEntities(u => u.UName == userName).FirstOrDefault(); if (userInfo != null) { if (WebCommon.ValidateUserInfoCookie(userInfo)) { //信息正确 跳转主界面 return(Redirect(Url.Action("Index", "Home"))); } } } } else { string sessionID = Request.Cookies["sessionID"].Value; //获取Memcache中的数据 object obj = MemcacheHelper.Get(sessionID); if (obj != null) { //反序列化存储在Memcache的用户 UserInfo userInfo = SerializeHelper.DeSerializeToT <UserInfo>(obj.ToString()); //模拟滑动过期时间。 MemcacheHelper.Set(sessionID, obj, DateTime.Now.AddMinutes(20)); return(Redirect(Url.Action("Index", "Home"))); } } return(View()); }
/// <summary> /// 检验用户是否有权限访问,并返回访问地址 /// </summary> /// <returns></returns> public static string CheckLoginUserAction(UserInfo loginUserInfo) { //return true; //每次的context都不一样!所以不能用这个类中的context Context = HttpContext.Current; //格式-->/xxx/xx(没有域名的,即没有http://localhost:61766/) string url = Context.Request.Url.AbsolutePath.ToLower();//数据库中存的是小写 if (url == "/") { //说明访问的是首页 return(url); } if (url.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries)[1] == null) { //说明访问的是控制器的index url += "/index"; } string httpMethod = Context.Request.HttpMethod.ToUpper();//数据库存的是大写 var actionInfo = actionInfoBll.LoadEntities(a => a.Url == url && a.HttpMethod == httpMethod).FirstOrDefault(); //系统中不存在此权限 if (actionInfo == null) { return(null); } var userInfo = userInfoBll.LoadEntities(u => u.ID == loginUserInfo.ID).FirstOrDefault(); //系统中不存在此用户 if (userInfo == null) { return(null); } //1.用户->权限 var isExist = (from a in userInfo.R_UserInfo_ActionInfo where a.ActionInfoID == actionInfo.ID select a).FirstOrDefault(); //用户存在此权限 if (isExist != null) { //此权限被禁止 if (!isExist.IsPass) { return(null); } else { return(url); } } //2.用户->角色->权限(能走到这,说明此权限没被禁止!) var userRole = userInfo.RoleInfo; var count = (from r in userRole from a in r.ActionInfo where a.ID == actionInfo.ID select a).Count(); if (count > 0) { return(url); } return(null); }
/// <summary> /// 执行控制器中的方法之前先执行该方法。 /// </summary> /// <param name="filterContext"></param> protected override void OnActionExecuting(ActionExecutingContext filterContext) { //base.OnActionExecuting(filterContext); //检查Session是否存在 //if (Session["UserInfo"] == null) if (Request["sessionID"] == null) { //检查Cookie储存的用户信息 if (Request.Cookies["UserName"] != null) { string userName = Request.Cookies["UserName"].Value; //IOC IApplicationContext ctx = ContextRegistry.GetContext(); IUserInfoBll userInfoBll = (IUserInfoBll)ctx.GetObject("UserInfoBll"); UserInfo userInfo = userInfoBll.LoadEntities(u => u.UName == userName).FirstOrDefault(); if (userInfo != null) { if (!WebCommon.ValidateUserInfoCookie(userInfo)) { //信息不正确 跳转登录页面(可以提醒用户Cookie失效) filterContext.Result = Redirect(Url.Action("Index", "Login")); } else { //Session["UserInfo"] = userInfo; WebCommon.SaveToMemcache(userInfo); LoginUserInfo = userInfo; if (LoginUserInfo.UName == "BlangenCen") { return; } //SessionID过期,且Cookie存有正确的用户信息,访问主页 string requestUrl = WebCommon.CheckLoginUserAction(userInfo); if (string.IsNullOrEmpty(requestUrl)) { //用户没有权限访问 filterContext.Result = Redirect("/NoAction.html"); } else { filterContext.Result = Redirect(requestUrl); } } } else { //并提醒用户Cookie失效 filterContext.Result = Redirect("/Login/Index"); } } else { //filterContext.Result = Redirect("/Login/Index"); filterContext.Result = Redirect(Url.Action("Index", "Login")); } } else { string sessionID = Request.Cookies["sessionID"].Value; //获取Memcache中的数据 object obj = MemcacheHelper.Get(sessionID); if (obj != null) { //反序列化存储在Memcache的用户 UserInfo userInfo = SerializeHelper.DeSerializeToT <UserInfo>(obj.ToString()); //模拟滑动过期时间。 MemcacheHelper.Set(sessionID, obj, DateTime.Now.AddMinutes(20)); LoginUserInfo = userInfo; if (LoginUserInfo.UName == "BlangenCen") { return; } string requestUrl = WebCommon.CheckLoginUserAction(userInfo); if (string.IsNullOrEmpty(requestUrl)) { //用户没有权限访问 filterContext.Result = Redirect("/NoAction.html"); } else { //filterContext.Result = Redirect(requestUrl); return;//这里直接return,就会开始执行方法了~ 不要滥用filterContext.Result!!! } } else { //没有用户信息,跳转登录页面 filterContext.Result = Redirect(Url.Action("Index", "Login")); } } }