public HomeController(IUserInfoService serviceParam, IActionInfoService actionServiceParam, ICacheWriter cacheWriterParam) { usersService = serviceParam; actionService = actionServiceParam; cacheWriter = cacheWriterParam; }
/// <summary> /// 过滤非菜单权限 /// </summary> /// <returns></returns> private bool FilteringPermissions() { string url = Request.Url.AbsolutePath.ToLower(); //用户想访问的地址 string httpMethod = Request.HttpMethod; //用户请求的方式 IApplicationContext ctx = ContextRegistry.GetContext(); IActionInfoService ActionInfoService = (IActionInfoService)ctx.GetObject("ActionInfoService"); //自动创建ActionInfoService实例 //查找权限表根据url和请求方式找出相应的权限 var actionInfo = ActionInfoService.LoadEntities(a => a.Url == url && a.HttpMethod == httpMethod).FirstOrDefault(); IUserInfoService UserInfoService = (IUserInfoService)ctx.GetObject("UserInfoService"); //自动创建UserInfoService实例 var loginUser = UserInfoService.LoadEntities(u => u.ID == LoginUser.ID).FirstOrDefault(); //1.用户--权限 var IsAllowAction = (from a in loginUser.R_UserInfo_ActionInfo where a.ActionInfoID == actionInfo.ID select a).FirstOrDefault(); if (IsAllowAction != null) //拥有该权限 { if (IsAllowAction.IsPass == true) //该权限没有被禁止 { return(true); } } //2.用户-角色-权限 var roleAction = (from r in loginUser.RoleInfo from a in r.ActionInfo where a.ID == actionInfo.ID select a).FirstOrDefault(); if (roleAction != null) { return(true); } return(false); }
public UserInfoController(IUserinfoService _userinfoService, IRoleInfoService _roleInfoService, IActionInfoService _actionInfoService, IR_UserInfo_ActionInfoService _r_UserInfo_ActionInfoService) { userinfoService = _userinfoService; roleInfoService = _roleInfoService; actionInfoservice = _actionInfoService; r_UserInfo_ActionInfoService = _r_UserInfo_ActionInfoService; }
public UserInfoController(IUserInfoService serviceParam, IRoleInfoService roleServiceParam, IR_UserInfo_ActionInfoService userActionServiceParam, IActionInfoService actionServiceParam) { userInfoService = serviceParam; roleInfoService = roleServiceParam; rUserActionService = userActionServiceParam; actionService = actionServiceParam; }
public UserInfoController(IUserInfoService us, IRoleInfoService rs, IActionInfoService af, IR_UsreInfo_ActionInfoService ira) { this.userInfoService = us; this.roleInfoService = rs; this.actionInfoService = af; this.r_UsreInfo_ActionInfoService = ira; }
public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); string strController = filterContext.RouteData.Values["controller"].ToString(); string strAction = filterContext.RouteData.Values["action"].ToString(); if (strController.Equals("Login") || strController.Equals("Error")) { return; // 如果访问的是 Login 就直接放回 } // 校验用户登录 if (filterContext.HttpContext.Request.Cookies["LoginID"] == null) { filterContext.HttpContext.Response.Redirect("/Login/Index"); return; } string loginId = filterContext.HttpContext.Request.Cookies["LoginID"].Value; if (string.IsNullOrEmpty(loginId)) { // 留一个后门,请求的 Contorller 是Articles 或 Search ,则忽略登录验证 if (strController.Equals("Articles") || strController.Equals("Search")) { return; } filterContext.HttpContext.Response.Redirect("/Login/Index"); return; } object loginUser = MemcacheHelper.Get(loginId); //SerializeHelper if (loginUser == null) { filterContext.HttpContext.Response.Redirect("/Login/Index"); return; } UserInfo userInfo = SerializeHelper.DeserializeToObject <UserInfo>(loginUser.ToString()); if (userInfo == null) { filterContext.HttpContext.Response.Redirect("/Login/Index"); return; } MemcacheHelper.Set(loginId, SerializeHelper.SerializeToString(userInfo), DateTime.Now.AddMinutes(20)); // 重新设置过期时间 即 平滑时间 // 校验用户权限 IApplicationContext ctx = ContextRegistry.GetContext(); IUserInfoService userInfoService = ctx.GetObject <UserInfoService>("UserInfoService"); IActionInfoService actionInfoService = ctx.GetObject <ActionInfoService>("ActionInfoService"); userInfo = userInfoService.LoadEntites(o => o.ID.Equals(userInfo.ID)).FirstOrDefault(); BaseController.LoginUser = userInfo; if (!actionInfoService.ValidateUserAction(userInfo, filterContext.HttpContext.Request)) { filterContext.HttpContext.Response.Redirect("/Error/Index/?msg=" + "无访问权限"); } }
/// <summary> /// 执行控制器中的方法之前先执行该方法。 /// </summary> /// <param name="filterContext"></param> protected override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); //if (Session["userInfo"] == null) bool isSucess = false; if (Request.Cookies["sessionId"] != null) { string sessionId = Request.Cookies["sessionId"].Value; //根据该值查Memcache. object obj = Common.MemcacheHelper.Get(sessionId); if (obj != null) { UserInfo userInfo = Common.SerializeHelper.DeserializeToObject <UserInfo>(obj.ToString()); LoginUser = userInfo; isSucess = true; Common.MemcacheHelper.Set(sessionId, obj, DateTime.Now.AddMinutes(20));//模拟出滑动过期时间. //留一个后门,测试方便。发布的时候一定要删除该代码。 if (LoginUser.UName == "msk") { //return; } //完成权限校验。 //获取用户请求的URL地址. string url = Request.Url.AbsolutePath.ToLower(); //获取请求的方式. string httpMehotd = Request.HttpMethod; //根据获取的URL地址与请求的方式查询权限表。 IApplicationContext ctx = ContextRegistry.GetContext(); IActionInfoService ActionInfoService = (IActionInfoService)ctx.GetObject("ActionInfoService");//因为在BaseController中不能通过Spring.net配置文件直接拿到UserInfoService,所以通过ContextRegistry.GetContext()方法创建一个IApplicationContext对象,通过该对象的GetObject方法,拿到配置文件Service.xml中的对象 var actionInfo = ActionInfoService.LoadEntities(a => a.Url == url && a.HttpMethod == httpMehotd).FirstOrDefault(); if (actionInfo == null) { filterContext.Result = Redirect("/Error.html"); return; } //判断用户是否具有所访问的地址对应的权限 IUserInfoService UserInfoService = (IUserInfoService)ctx.GetObject("UserInfoService"); var loginUserInfo = UserInfoService.LoadEntities(u => u.ID == LoginUser.ID).FirstOrDefault(); //1:可以先按照用户权限这条线进行过滤。 var isExt = (from a in loginUserInfo.R_UserInfo_ActionInfo where a.ActionInfoID == actionInfo.ID select a).FirstOrDefault(); if (isExt != null) { if (isExt.IsPass) { return; } else { filterContext.Result = Redirect("/Error.html"); return; } } //2:按照用户角色权限这条线进行过滤。 var loginUserRole = loginUserInfo.RoleInfo; var count = (from r in loginUserRole from a in r.ActionInfo where a.ID == actionInfo.ID select a).Count(); if (count < 1) { filterContext.Result = Redirect("/Error.html"); return; } } // filterContext.HttpContext.Response.Redirect("/Login/Index"); } if (!isSucess) { filterContext.Result = Redirect("/Login/Index");//注意. } }
/// <summary> /// 执行控制器中的方法之前先执行该方法。进行登录校验和权限校验 /// </summary> /// <param name="filterContext"></param> protected override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); bool isSucess = false; if (Request.Cookies["sessionId"] != null) { string sessionId = Request.Cookies["sessionId"].Value; //根据该值查Memcache中保存的数据 object obj = MemcacheHelper.Get(sessionId); if (obj != null) { UserInfo userInfo = SerializeHelper.DeserializeToObject <UserInfo>(obj.ToString()); LoginUser = userInfo; isSucess = true; MemcacheHelper.Set(sessionId, obj, DateTime.Now.AddMinutes(20)); //模拟出滑动过期时间. #region 非菜单权限过滤 //为了方便测试加的后门 if (LoginUser.UName == "admin") { return; } //获取当前请求Url地址 string url = Request.Url.AbsolutePath; if (url.Equals("/", StringComparison.CurrentCultureIgnoreCase) || url.Equals("/Home/Index", StringComparison.CurrentCultureIgnoreCase) || url.Equals("/Home/GetMenu", StringComparison.CurrentCultureIgnoreCase) || url.Equals("/Home/HomePage", StringComparison.CurrentCultureIgnoreCase)) { return; } //获取请求方式 string httpMethod = Request.HttpMethod; IApplicationContext ctx = ContextRegistry.GetContext(); //查找访问页面的权限信息 IActionInfoService ActionInfoService = (IActionInfoService)ctx.GetObject("ActionInfoService"); var actionInfo = ActionInfoService.LoadEntities(a => a.Url == url && a.HttpMethod == httpMethod).FirstOrDefault(); if (actionInfo == null) { filterContext.Result = Redirect("/Error.html"); return; } //查找登录用户是否具有访问权限 IUserInfoService UserInfoService = (IUserInfoService)ctx.GetObject("UserInfoService"); var loginUser = UserInfoService.LoadEntities(u => u.ID == LoginUser.ID).FirstOrDefault(); //1、 //var isExt = LoginUser.R_UserInfo_ActionInfo.Where(a => a.ActionInfoID == actionInfo.ID).FirstOrDefault(); var isExt = (from a in loginUser.R_UserInfo_ActionInfo where a.ActionInfoID == actionInfo.ID select a).FirstOrDefault(); if (isExt != null) { if (isExt.IsPass) { return; } else { filterContext.Result = Redirect("/Error.html"); return; } } //2、 var userRole = loginUser.RoleInfo; var roleAction = (from r in userRole from a in r.ActionInfo where a.ID == actionInfo.ID select a).Count(); if (roleAction < 1) { filterContext.Result = Redirect("/Error.html"); return; } #endregion } //不会返回一个ActionResult对象 还会继续运行下面的代码 //filterContext.HttpContext.Response.Redirect("/Login/Index"); } if (!isSucess) { //返回了ActionResult对象 直接跳转 不执行下面的代码 filterContext.Result = Redirect("/Login/Index"); //注意. } }
protected override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); if (IsCheckUserLogin) { #region 用户登陆校验 //Memchache+Cookie方式 if (Request.Cookies["userLoginId"] == null) { filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); return; } string userGuid = Request.Cookies["userLoginId"].Value; UserInfo userInfo = (UserInfo)Common.Cache.CacheHelper.GetCache (userGuid);//as UserInfo if (userInfo == null) { //长时间为操作 缓存已超时 filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); return; } LoginUser = userInfo; //滑动窗口机制 (响应后刷新缓冲时间) Common.Cache.CacheHelper.SetCache(userGuid, userInfo, DateTime.Now.AddMinutes(20)); #region Session方式 //if (filterContext.HttpContext.Session["LoginUser"] == null && IsCheckedUserLogin) //{ // filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); //} //else //{ // LoginUser= filterContext.HttpContext.Session["LoginUser"] as UserInfo; //} #endregion #endregion #region 用户权限校验 //校验权限 //获取当前请求对应的权限数据 if (LoginUser.UName == "Moshang") { return;//Moshang`s backdoor } string url = Request.Url.AbsolutePath; string[] splitArr = url.Split('/'); string newStrurl = splitArr[0] + "/" + splitArr[1] + "/" + splitArr[2]; string httpMethod = Request.HttpMethod.ToLower(); //通过容器获取 IApplicationContext ctx = ContextRegistry.GetContext(); //ctx.GetObject("CacheHelper"); IActionInfoService actionInfoService = ctx.GetObject("ActionInfoService") as IActionInfoService; IR_UserInfo_ActionInfoService rUserInfoActionInfoService = ctx.GetObject("R_UserInfo_ActionInfoService") as IR_UserInfo_ActionInfoService; IUserInfoService UserInfoService = ctx.GetObject("UserInfoService") as IUserInfoService; //真·奥义·权限校验 var actionInfo = actionInfoService.GetEntities(a => a.Url.ToLower() == newStrurl && a.HttpMethd.ToLower() == httpMethod).FirstOrDefault(); if (actionInfo == null) { Response.Redirect("/Error.html"); } var rUAs = rUserInfoActionInfoService.GetEntities(u => u.UserInfoID == LoginUser.ID); var item = (from a in rUAs where a.ActionInfoID == actionInfo.ID select a).FirstOrDefault(); if (item != null) { if (item.HasPermission == true) { return; } else { Response.Redirect("/Error.html"); } } //真·奥义·角色校验 var user = UserInfoService.GetEntities(u => u.ID == LoginUser.ID).FirstOrDefault(); var allRoles = from r in user.RoleInfo select r; var actions = from r in allRoles from a in r.ActionInfo select a; var temp = (from a in actions where a.ID == actionInfo.ID select a).Count(); if (temp <= 0) { Response.Redirect("/Error.html"); } #endregion } }
public ActionInfoController(IActionInfoService actioninfo) { this.actionInfoService = actioninfo; }
protected override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); IApplicationContext ctx = ContextRegistry.GetContext();//读取sping.net配置信息,创建容器。 IUserInfoService userInfoService = (IUserInfoService)ctx.GetObject("UserInfoService"); //if (Session["userInfo"] == null) if (Request.Cookies["sessionId"] == null) { //filterContext.HttpContext.Response.Redirect("/Login/Index"); if (Request.Cookies["cp1"] != null) { string userName = Request.Cookies["cp1"].Value; //获得cookies中存的用户名 //判断用户名是不是正确 UserInfo userInfo = userInfoService.LoadEntities(u => u.UName == userName).FirstOrDefault(); if (!Common.WebCommon.ValidateCookieInfo(userInfo)) { filterContext.Result = Redirect(Url.Action("Index", "Login")); return; } LoginUser = userInfo; } else { filterContext.Result = Redirect(Url.Action("Index", "Login")); return; } } else //如果有值就取出来 { string sessionId = Request.Cookies["sessionId"].Value; object obj = Common.MemcacheHelper.Get(sessionId); //获取memcache中的数据 if (obj != null) { UserInfo userInfo = Common.SerializeHelper.DeserializeToObject <UserInfo>(obj.ToString()); //反序列化 LoginUser = userInfo; //模拟滑动过期时间 Common.MemcacheHelper.Set(sessionId, obj, DateTime.Now.AddMinutes(20)); } else { filterContext.Result = Redirect(Url.Action("Index", "Login")); return; } } //过滤非菜单权限 if (LoginUser != null) { //string url1 = Request.Url.AbsolutePath.ToString().ToLower(); //获取当前请求的URL地址 //留后门,发布一定要删除 if (LoginUser.UName == "itcast") { return; } string url = Request.Url.AbsolutePath.ToString().ToLower(); //获取当前请求的URL地址 string httpMethod = Request.HttpMethod; //获取请求的方式 //查找url地址对应的权限信息 IActionInfoService actionInfoService = (IActionInfoService)ctx.GetObject("ActionInfoService"); var actionInfo = actionInfoService.LoadEntities(a => a.Url == url && a.HttpMethod == httpMethod).FirstOrDefault(); if (actionInfo == null) { filterContext.Result = Redirect("/Error.html"); return; } //判断登录用户是否有actionInfo的访问权限。 //也是按照两条线进行过滤。 //1先按照用户-->权限这条进行过滤. var userInfo = userInfoService.LoadEntities(u => u.ID == LoginUser.ID).FirstOrDefault(); //获取登陆用户信息 var userAction = (from a in userInfo.R_UserInfo_ActionInfo where a.ActionInfoID == actionInfo.ID select a).FirstOrDefault(); if (userAction != null) //如果成立,表示登录用户有userInfo这个权限,但是考虑是否 被禁止。 { if (userAction.IsPass) //表示允许,后面就不要校验了,直接访问用户请求的Url地址。 { return; } else { filterContext.Result = Redirect("/Error.html"); return; } } //2:按照用户-->角色--->权限进行校验. var loginUserRoles = userInfo.RoleInfo; var loginUserAction = (from r in loginUserRoles from a in r.ActionInfo where a.ID == actionInfo.ID select a).Count(); if (loginUserAction < 1) { filterContext.Result = Redirect("/Error.html"); return; } } }
//在当前控制器所有方法执行之前执行此代码 protected override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); //登录时不需要验证是否登录 //#region 测试信息 ////TODO:测试结束后删除 //return; //#endregion if (IsCheck) { //从mm缓存中读取数据 if (Request.Cookies["loginuserId"] == null) { filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); return; } string userGuid = Request.Cookies["loginuserId"].Value.ToString(); UserInfo user = Common.Cache.CacheHelper.GetCache(userGuid) as UserInfo; //用户长时间不进行操作,超时了 if (user == null) { filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); return; } LoginUserInfo = user; //设置滑动窗口机制,一旦登陆了,就给当前用户+20min Common.Cache.CacheHelper.SetCache(userGuid, user, DateTime.Now.AddMinutes(20)); //给admin留后门,首页查询权限之后直接显示图标 if (LoginUserInfo.Uname == "admin") { return; } else { string url = Request.Url.AbsolutePath.ToLower(); string httpMethod = Request.HttpMethod.ToLower(); //通过一个容器创建对象 IApplicationContext ctx = ContextRegistry.GetContext(); IActionInfoService actionInfoService = ctx.GetObject("ActionInfoService") as IActionInfoService; IR_UserInfo_ActionInfoService r_UserInfo_ActionInfoService = ctx.GetObject("R_UserInfo_ActionInfoService") as IR_UserInfo_ActionInfoService; IUserInfoService userInfoService = ctx.GetObject("UserInfoService") as IUserInfoService; var actionInfo =//拿到当前请求对应的权限 actionInfoService.GetEntities(u => u.Url.ToLower() == url && u.HttpMethod.ToLower() == httpMethod).FirstOrDefault(); if (actionInfo == null) { Response.Redirect("/Error.html"); } #region 第一条线 var action = r_UserInfo_ActionInfoService.GetEntities(u => u.UserInfoId == LoginUserInfo.Id); var item = (from s in action where s.ActionInfoId == actionInfo.Id select s).FirstOrDefault(); if (item != null) { if (item.HasPermission == true) { return; } else { Response.Redirect("/Error.html"); } } #endregion #region 第二条线 var userinfo = userInfoService.GetEntities(u => u.Id == LoginUserInfo.Id).FirstOrDefault(); //拿到所有角色 var roles = from r in userinfo.RoleInfo select r; //拿到所有角色对应的权限 var actions = from r in roles from a in r.ActionInfo select a; //当前权限是否在角色对应的权限集合中 var temp = (from a in actions where a.Id == actionInfo.Id select a).Count(); if (temp <= 0) { Response.Redirect("/Error.html"); } #endregion } } }
//执行控制器方法之前先执行该方法 protected override void OnActionExecuting(ActionExecutingContext filterContext) { bool isExt = false; if (Request.Cookies["sessionID"] != null) { string sessionID = Request.Cookies["sessionID"].Value; object obj=Common.MemcacheHelper.Get(sessionID); if (obj != null) { LoginUser=Common.SerializerHelper.DeSerializerToObject<UserInfo>(obj.ToString()); isExt = true; //完成权限过滤 if (LoginUser.UName == "itcast") { return; } string requstUrl = Request.Url.AbsolutePath.ToLower();//获取url string requestMethod = Request.HttpMethod.ToLower();//获取请求方式 IApplicationContext ctx = ContextRegistry.GetContext(); IUserInfoService userInfoService = (IUserInfoService)ctx.GetObject("userInfoService"); IActionInfoService actionInfoService=(IActionInfoService)ctx.GetObject("actionInfoService"); var currentAction = actionInfoService.LoadEntities(a => a.Url.ToLower() == requstUrl && a.HttpMethod.ToLower() == requestMethod).FirstOrDefault(); if (currentAction == null) { Response.Redirect("/Error.html"); return; } //通过1号线进行校验 var userInfo = userInfoService.LoadEntities(u => u.ID == LoginUser.ID).FirstOrDefault(); var actions = userInfo.R_UserInfo_ActionInfo.Where(r => r.ActionInfoID == currentAction.ID).FirstOrDefault(); if (actions != null) { if (actions.IsPass == true) { return; } else { Response.Redirect("/actioninfo.html"); return; } } //2号线 var currentUserRoles = userInfo.RoleInfo; var currentUserActions = from a in currentUserRoles select a.ActionInfo; var count = (from a in currentUserActions from b in a where b.ID == currentAction.ID select b).Count(); if (count < 1) { Response.Redirect("/actioninfo.html"); return; } } } if (!isExt)//用户没有登录 { filterContext.HttpContext.Response.Redirect("/Login/Index"); } base.OnActionExecuting(filterContext); }
public ActionInfoController(IActionInfoService serviceParam, IRoleInfoService roleInfoServiceParam) { actionInfoService = serviceParam; roleInfoService = roleInfoServiceParam; }
/// <summary> /// 这个方法是在Action执行之前调用 /// </summary> /// <param name="filterContext"></param> public void OnAuthentication(AuthenticationContext filterContext) { //if (filterContext.HttpContext.Session["userInfo"] == null) //{ // //var Url = new UrlHelper(filterContext.RequestContext); // //var url = Url.Action("Logon", "Account", new { area = "" }); // //filterContext.Result = new RedirectResult(url); // filterContext.Result = new RedirectResult("/Login/Index"); //} bool isSucess = false; if (filterContext.HttpContext.Request.Cookies["sessionId"] != null) { string sessionId = filterContext.HttpContext.Request.Cookies["sessionId"].Value; //根据该值查Memcache. object obj = MemcacheHelper.Get(sessionId); if (obj != null) { UserInfo userInfo = SerializeHelper.DeserializeToObject <UserInfo>(obj.ToString()); LoginUser = userInfo; isSucess = true; MemcacheHelper.Set(sessionId, obj, DateTime.Now.AddMinutes(20)); //模拟出滑动过期时间. //留一个后门,测试方便。发布的时候一定要删除该代码。 if (LoginUser.UserName == "admin") { return; } //完成权限校验。 //获取用户请求的URL地址. string url = filterContext.HttpContext.Request.Url.AbsolutePath.ToLower(); //获取请求的方式. string httpMehotd = filterContext.HttpContext.Request.HttpMethod; //根据获取的URL地址与请求的方式查询权限表。 IApplicationContext ctx = ContextRegistry.GetContext(); IActionInfoService ActionInfoService = (IActionInfoService)ctx.GetObject("ActionInfoService"); var actionInfo = ActionInfoService.LoadEntities(a => a.Url == url && a.HttpMethod == httpMehotd).FirstOrDefault(); //判断用户是否具有所访问的地址对应的权限 IUserInfoService UserInfoService = (IUserInfoService)ctx.GetObject("UserInfoService"); var loginUserInfo = UserInfoService.LoadEntities(o => o.Id == LoginUser.Id).FirstOrDefault(); //1:可以先按照用户权限这条线进行过滤。 var isExt = (from a in loginUserInfo.UserInfo_ActionInfo where a.ActionInfoId == actionInfo.Id select a).FirstOrDefault(); if (isExt != null) { if (isExt.IsPass) { return; } else { filterContext.Result = new RedirectResult("/Error.html"); return; } } //2:按照用户角色权限这条线进行过滤。 var loginUserRole = loginUserInfo.RoleInfo_UserInfo; var count = (from r in loginUserRole from a in r.RoleInfo.RoleInfo_ActionInfo where a.ActionInfo.Id == actionInfo.Id select a).Count(); if (count < 1) { filterContext.Result = new RedirectResult("/Error.html"); return; } } } if (!isSucess) { filterContext.Result = new RedirectResult("/Login/Index"); //注意. } }
protected override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); //Action执行之前判断memcache是否有值(用户是否已经登录) if (Request.Cookies["usersLoginId"] != null) { string usersLoginId = Request.Cookies["usersLoginId"].Value; object obj = memcachedHelper.Get(usersLoginId); if (obj != null) { userInfo = SerializerHelper.DeSerializerToObject <UserInfo>(obj.ToString());//反序列化 //模拟Session的滑动过期时间 memcachedHelper.Update(usersLoginId, obj, DateTime.Now.AddMinutes(20)); isExp = true; //zhengyu可越狱 if (userInfo.UName == "zhengyu") { return; } //获取请求的绝对路径和请求方式 string requestUrl = Request.Url.AbsolutePath.ToLower(); string httpMethod = Request.HttpMethod; //通过容器对象来创建对象,因基类注入不了 IApplicationContext ctx = ContextRegistry.GetContext(); IActionInfoService actionInfoService = ctx.GetObject("ActionInfoService") as IActionInfoService; IUserInfoService UserInfoService = ctx.GetObject("UserInfoService") as IUserInfoService; ActionInfo actionInfo = actionInfoService.GetEntities(a => a.Url == requestUrl && a.HttpMethd == httpMethod && a.DelFlag == (short)DelFlagEnum.Normal).FirstOrDefault(); if (actionInfo == null) { filterContext.Result = new RedirectResult("/Error.html"); //Response.Redirect("/Error.html"); return; } //第1条线.用户---权限 //登录用户 UserInfo loginUser = UserInfoService.GetEntities(u => u.ID == userInfo.ID).FirstOrDefault(); //判断登录用户请求的地址是否有权限 ActionInfo userActionOne = (from r in loginUser.R_UserInfo_ActionInfo where r.ActionInfoID == actionInfo.ID && r.HasPermission == true select r.ActionInfo).FirstOrDefault(); if (userActionOne == null) { //第2条线.用户---角色---权限 //判断登录用户请求的地址是否有权限 ActionInfo userActionTwo = (from r in loginUser.RoleInfo from a in r.ActionInfo where a.ID == actionInfo.ID select a).FirstOrDefault(); if (userActionTwo == null) { //filterContext.Result = new RedirectResult("/ActionError.html"); //Response.Redirect("/ActionError.html"); filterContext.Result = new ContentResult() { Content = "您没有此权限!请联系管理员" }; return; } } } } if (!isExp) { RedirectToAct.RedirectTo(); //filterContext.HttpContext.Response.Redirect("/UserLogin/Index?return="+Request.Url); return; } }
public MyActionFilterAttribute(IActionInfoService aa, IRoleInfoService rs) { this.ActionInfoService = aa; this.RoleInfoService = rs; }
//执行控制器的方法之前先执行该方法 //这是另外一种使用方法过滤器的方法 protected override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); bool isSus = false; if (Request.Cookies["sesId"] != null) { //filterContext.HttpContext.Response.Redirect("/Login/Index"); //必须要拿到一个actionresult 如果用这个方法 还会往下走 //没有返回result 会继续走 //filterContext.Result = Redirect("/Login/Index"); string sesId = Request.Cookies["sesId"].Value; object obj = MemcacheHelper.Get(sesId); if (obj != null) { UserInfo userInfo = SerializeHelper.DeserializeToObject <UserInfo>(obj.ToString()); LoginUser = userInfo; isSus = true; MemcacheHelper.Set(sesId, obj, DateTime.Now.AddMinutes(20));//模拟滑动过期时间 //先留个后门方便测试 这个用户登录的话 后面的都不走了 项目做完了 这个要删除掉 if (LoginUser.UName == "326209") { return; } //完成权限校验 //获取用户请求的URL地址 string url = Request.Url.AbsolutePath; //获取请求方式 string httpMethod = Request.HttpMethod; //根据获取的url地址与请求方式查看用户是否有访问权限 IApplicationContext ctx = ContextRegistry.GetContext(); IUserInfoService userInfoService = (IUserInfoService)ctx.GetObject("UserInfoService"); IActionInfoService actionInfoService = (IActionInfoService)ctx.GetObject("ActionInfoService"); var actionInfo = actionInfoService.LoadEntities(a => a.HttpMethod == httpMethod && a.Url == url.ToLower()).FirstOrDefault(); var loginUserInfo = userInfoService.LoadEntities(u => u.ID == LoginUser.ID).FirstOrDefault(); //先按照用户权限这条线进行过滤 var isExe = (from a in loginUserInfo.R_UserInfo_ActionInfo where a.ActionInfoID == actionInfo.ID select a).FirstOrDefault(); if (isExe != null) { if (isExe.IsPass) { return; } else { filterContext.Result = Redirect("/error.html"); return; } } else { //按照第二条线过滤 var loginRole = loginUserInfo.RoleInfo; var count = (from r in loginRole from a in r.ActionInfo where a.ID == actionInfo.ID select a).Count(); if (count < 1) { filterContext.Result = Redirect("/error.html"); return; } } } } if (isSus == false) { filterContext.Result = Redirect("/Login/Index"); } }
// GET: Base /// <summary> /// 执行控制器中的方法之前先执行该方法 /// </summary> /// <param name="filterContext"></param> protected override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); bool isSuccess = false; if (Session["userInfo"] == null) { // filterContext.HttpContext.Response.Redirect("/Login/Index"); filterContext.Result = Redirect("/Login/Index"); } else { LoginUser = (UserInfo)Session["userInfo"]; if (LoginUser.UName == "HLX")//后门 { return; } isSuccess = true; //当前 string url = Request.Url.AbsolutePath;//获取当前路径的绝对路径 //请求 方式 string httpMethod = Request.HttpMethod; //根据获取url地址 与请求方式查询权限表 IApplicationContext ctx = ContextRegistry.GetContext();//拿到容器 IActionInfoService lister = (IActionInfoService)ctx.GetObject("ActionInfoService"); var actioninfo = lister.LoadEntities(a => a.Url == url && a.HttpMethod == httpMethod).FirstOrDefault(); //判断 用户是否具有所访问 的地址对应的限制 if (actioninfo == null) { return; } IUserInfoService userinfoService = (IUserInfoService)ctx.GetObject("UserInfoService"); var longUserInfo = userinfoService.LoadEntities(u => u.ID == LoginUser.ID).FirstOrDefault(); //按照用户权限 进行过滤 var isExt = (from a in LoginUser.R_UserInfo_ActionInfo where a.ActionInfoID == actioninfo.ID select a).FirstOrDefault(); if (isExt != null) { if (isExt.IsPass) { return; } else { filterContext.Result = Redirect("/Error.html"); return; } } //按照角色权限进行过滤 var loginUserRole = longUserInfo.RoleInfo; var count = (from r in loginUserRole from a in r.ActionInfo where a.ID == actioninfo.ID select a).Count(); if (count < 1) { filterContext.Result = Redirect("/Error/html"); return; } } }
protected override void OnActionExecuting(ActionExecutingContext filterContext) { //mvc请求来了之后,根据请求地址,创建控制器工厂(Spring.Net),控制器工厂创建控制器,执行方法。 //Spring.Net base.OnActionExecuting(filterContext); var items = filterContext.RouteData.Values; if (IsCheckUserLogin) { //使用mm+cookie代替session //校验用户是否已经登录 //从缓存中拿到当前的登录的用户信息。 if (Request.Cookies["userLoginId"] == null) { filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); return; } string userGuid = Request.Cookies["userLoginId"].Value; UserInfo userInfo = Common.Cache.CacheHelper.GetCache(userGuid) as UserInfo; if (userInfo == null) { //用户长时间不操作,。超时。 filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); return; } LoginUser = userInfo; //滑动窗口机制。 Common.Cache.CacheHelper.SetCache(userGuid, userInfo, DateTime.Now.AddMinutes(20)); //if (filterContext.HttpContext.Session["loginUser"] == null) //{ // filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); //} //else //{ // LoginUser = filterContext.HttpContext.Session["loginUser"] as UserInfo; //} //校验权限 //把当前请求对应的权限数据拿到。 if (LoginUser.UName == "admin") { return;//侯梦 } string url = Request.Url.AbsolutePath.ToLower(); string httpMethod = Request.HttpMethod.ToLower(); //默认都有流程的操作了 if (url.Contains("WFInstance".ToLower())) { return; } bool isGetMethodWithParameter = url.Count(ch => ch == '/') != 2; if (isGetMethodWithParameter) { int lastIndex = url.LastIndexOf('/'); url = url.Substring(0, lastIndex); } //通过容器创建一个对象。 IApplicationContext ctx = ContextRegistry.GetContext(); IActionInfoService actionInfoService = ctx.GetObject("ActionInfoService") as IActionInfoService; IR_UserInfo_ActionInfoService rUserInfoActionInfoService = ctx.GetObject("R_UserInfo_ActionInfoService") as IR_UserInfo_ActionInfoService; IUserInfoService UserInfoService = ctx.GetObject("UserInfoService") as IUserInfoService; var actionInfo =//拿到当前请求对应的权限数据 actionInfoService.GetEntities(a => a.DelFlag == DeleteFlag.DelflagNormal && a.Url.ToLower() == url && a.HttpMethd.ToLower() == httpMethod) .FirstOrDefault(); if (actionInfo == null) { Response.Redirect("/Error.html"); } //一号线 var rUAs = rUserInfoActionInfoService.GetEntities(u => u.DelFlag == DeleteFlag.DelflagNormal && u.UserInfoID == LoginUser.ID); var item = (from a in rUAs where a.ActionInfoID == actionInfo.ID select a).FirstOrDefault(); if (item != null) { if (item.HasPermission == true) { return; } else { Response.Redirect("/Error.html"); } } //2号 var user = UserInfoService.GetEntities(u => u.DelFlag == DeleteFlag.DelflagNormal && u.ID == LoginUser.ID).FirstOrDefault(); //拿到所有的角色 var allRoles = from r in user.RoleInfo select r; //通过角色拿到所有的权限 var actions = from r in allRoles from a in r.ActionInfo select a; //看当前权限是否在 角色对应权限集合中。 var temp = (from a in actions where a.ID == actionInfo.ID select a).Count(); if (temp <= 0) { Response.Redirect("/Error.html"); } } }
public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); if (IsCheckUserLogin) { string cookie = Utils.GetCookie("userLoginId"); if (string.IsNullOrEmpty(cookie)) { if (IsAdmin) { filterContext.HttpContext.Response.Redirect("/UserLogin/AdminLogin"); return; } filterContext.HttpContext.Response.Redirect("/UserLogin/Login"); return; } string userGuid = cookie; UserInfo userInfo = Common.Cache.CacheHelper.GetCache <UserInfo>(userGuid); if (userInfo == null) { if (IsAdmin) { filterContext.HttpContext.Response.Redirect("/UserLogin/AdminLogin"); return; } filterContext.HttpContext.Response.Redirect("/Content/bigdatalogin/bigdatalogin.html"); return; } //滑动窗口机制 Common.Cache.CacheHelper.SetCache(userGuid, userInfo, DateTime.Now.AddMinutes(20)); if (IsRoleAction) { string url = HttpContext.Current.Request.Url.AbsolutePath.ToLower(); string[] str = url.Split('/'); string URL = "/" + str[1] + "/" + str[2]; string httpMethod = HttpContext.Current.Request.HttpMethod.ToLower(); IApplicationContext ctx = ContextRegistry.GetContext(); IActionInfoService ActionInfoService = ctx.GetObject("ActionInfoService") as IActionInfoService; IUserActionInfoService UserActionInfoService = ctx.GetObject("UserActionInfoService") as IUserActionInfoService; IRoleInfoService RoleInfoService = ctx.GetObject("RoleInfoService") as IRoleInfoService; IUserInfoService UserInfoService = ctx.GetObject("UserInfoService") as IUserInfoService; var actionInfo = ActionInfoService.GetEntity(a => a.Url.ToLower() == URL && a.HttpMethd.ToLower() == httpMethod).FirstOrDefault(); if (actionInfo == null) { actionInfo = ActionInfoService.Add(URL, httpMethod); //HttpContext.Current.Response.Redirect("/Error.html"); } if (userInfo.UserName == "admin") { return; } var rUAs = UserActionInfoService.GetEntity(u => u.UserInfoID == userInfo.ID); var item = (from a in rUAs where a.ActionInfoID == actionInfo.ID select a).FirstOrDefault(); if (item != null) { if (item.HasPermissin == 1 && item.DelFlag == true) { return; } else { HttpContext.Current.Response.Redirect("/Error.html"); } } var user = UserInfoService.GetEntity(u => u.ID == userInfo.ID).FirstOrDefault(); var allRoles = from r in user.RoleInfo select r; var actions = from r in allRoles from a in r.ActionInfo select a; var temp = (from a in actions where a.ID == actionInfo.ID select a).Count(); if (temp <= 0) { HttpContext.Current.Response.Redirect("/Error.html"); } } } }
/// <summary> /// Area of Influence: internal of the inherited controller /// This method will run before other methods /// </summary> protected override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); if (ActivateCheck) { #region Store Login GUID in Cache // use memcache-Cookie instead of session if (Request.Cookies["userLoginGuid"] == null) { filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); } string userGuid = Request.Cookies["userLoginGuid"].Value; UserInfo userInfo = Common.Cache.CacheHelper.GetCache(userGuid) as UserInfo; if (userInfo == null) { // The cache data is expired/overtime, please login again filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); } LoginUser = userInfo; // Extend the cache time for 20 minutes Common.Cache.CacheHelper.SetCache(userGuid, userInfo, DateTime.Now.AddMinutes(20)); #endregion #region Permission check if (LoginUser.UserName == "a") { return; } string currentUrl = Request.Url.AbsolutePath.ToLower(); string currentHttpMethod = Request.HttpMethod.ToLower(); IApplicationContext ctx = ContextRegistry.GetContext(); IActionInfoService ActionInfoService = ctx.GetObject("ActionInfoService") as IActionInfoService; IR_UserInfo_ActionInfoService R_UserInfo_ActionInfoService = ctx.GetObject("R_UserInfo_ActionInfoService") as R_UserInfo_ActionInfoService; IUserInfoService UserInfoService = ctx.GetObject("UserInfoService") as IUserInfoService; // 1 check if the user has a special permission to the url with the httpmethod // check if the Url and the HttpMethod exist var actionInfo = ActionInfoService.GetEntities(a => a.Url.ToLower() == currentUrl && a.HttpMethod.ToLower() == currentHttpMethod).FirstOrDefault(); if (actionInfo == null) { // Url or HttpMethod not exist Response.Redirect("/Error.html"); } // check if the current user has permission to the page with the httpmethod var rUAInfo = R_UserInfo_ActionInfoService.GetEntities(u => u.UserInfoId == LoginUser.Id && u.ActionInfoId == actionInfo.Id && u.DelFlag == (short)DelFlagEnum.Normal).FirstOrDefault(); if (rUAInfo != null) { if (rUAInfo.HasPermission == true) { return; } else { Response.Redirect("/Error.html"); } } // 2 Check whether the user's corresponding role has this permission var user = UserInfoService.GetEntities(u => u.Id == LoginUser.Id && u.DelFlag == (short)DelFlagEnum.Normal).FirstOrDefault(); // get all user roles var allRoles = from r in user.RoleInfo select r; // get all role actions var actions = from r in allRoles from a in r.ActionInfo select a; // Detect if there is a role-action that matches the current action var result = (from a in actions where a.Id == actionInfo.Id select a).Count(); if (result <= 0) { Response.Redirect("/Error.html"); } #endregion #region Use Session for login check //if (filterContext.HttpContext.Session["loginUser"] == null) //{ // filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); //} //else //{ // LoginUser = filterContext.HttpContext.Session["loginUser"] as UserInfo; //} #endregion } }
protected override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); IApplicationContext ctx = ContextRegistry.GetContext(); if (IsCheck) { //从Redis缓存中读取数据 if (Request.Cookies["loginuserId"] == null) { filterContext.HttpContext.Response.Redirect("/adminlogin/Login.html"); return; } string userGuid = Request.Cookies["loginuserId"].Value.ToString(); object id = CacheHelper.CacheHelper.GetString(userGuid); //用户长时间不进行操作,超时了 if (id == null) { filterContext.HttpContext.Response.Redirect("/adminlogin/Login.html"); } int userid = int.Parse(id.ToString()); IUserInfoService userInfoService = ctx.GetObject("UserInfoService") as IUserInfoService; UserInfo userInfo = userInfoService.GetEntities(u => u.Id == userid).FirstOrDefault(); //将查出的用户赋值给当前登录用户 LoginUserInfo = userInfo; //设置滑动窗口机制,一旦登陆了,就给当前用户+20min CacheHelper.CacheHelper.SetCache(userGuid, userid, DateTime.Now.AddMinutes(20)); //给admin留后门 if (LoginUserInfo.UserName == "admin") { return; } else { string url = Request.Url.AbsolutePath.ToLower(); string httpmethod = Request.HttpMethod.ToLower(); IActionInfoService actionInfoService = ctx.GetObject("ActionInfoService") as IActionInfoService; IR_UserInfo_ActionInfoService r_UserInfo_ActionInfoService = ctx.GetObject("R_UserInfo_ActionInfoService") as IR_UserInfo_ActionInfoService; var actioninfo = actionInfoService.GetEntities(a => a.HttpMethod.ToLower() == httpmethod && a.Url.ToLower() == url).FirstOrDefault(); if (actioninfo == null) { ContentResult content = new ContentResult(); content.ContentType = "text/javascript"; content.Content = "{data:500}"; filterContext.Result = content; } else { //第一条线,直接去判断这个权限是否属于登录用户 //1、首先拿到用户所拥有的权限 var actionlist = r_UserInfo_ActionInfoService.GetEntities(u => u.UserInfoId == LoginUserInfo.Id); //拿到要访问的那一条权限 var visitAction = (from r in actionlist where r.ActionInfoId == actioninfo.Id select r).FirstOrDefault(); if (visitAction != null) { //3、判断该条权限是否被允许 if (visitAction.HasPermission == true) { return; } else { ContentResult content = new ContentResult(); content.ContentType = "text/javascript"; content.Content = "{data:500}"; filterContext.Result = content; } } //第二条线 //1、先拿到该用户所有的角色 var userinfo = userInfoService.GetEntities(u => u.Id == LoginUserInfo.Id).FirstOrDefault(); var allroles = from r in userinfo.RoleInfo select r; //拿到这些角色所拥有的权限 var actions = from r in allroles from a in r.ActionInfo select a; //当前权限是否在角色对应的权限集合中 var count = (from a in actions where a.Id == actioninfo.Id select a).Count(); if (count <= 0) { ContentResult content = new ContentResult(); content.ContentType = "text/javascript"; content.Content = "{data:500}"; filterContext.Result = content; } } } } }
public override void OnActionExecuting(ActionExecutingContext filterContext) { filterContext.HttpContext.Response.AppendHeader("P3P", "CP=CAO PSA OUR"); base.OnActionExecuting(filterContext); //if (IsCheck) //{ // //检验用户是否登陆 // if (filterContext.HttpContext.Session["LoginUser"] == null) // { // filterContext.HttpContext.Response.Redirect("/Login/Index"); // } //} //else //{ // LoginUser = filterContext.HttpContext.Session["LoginUser"] as UserInfo; //} if (IsCheck) { if (filterContext.HttpContext.Request.Cookies["userid"] == null) { filterContext.HttpContext.Response.Redirect("/Login/Index"); } else { string userid = filterContext.HttpContext.Request.Cookies["userid"].Value.ToString(); var s = CacheHelper.GetCache("userid"); if (CacheHelper.GetCache(userid) != null) { LoginUser = (UserInfo)CacheHelper.GetCache(filterContext.HttpContext.Request.Cookies["userid"].Value); //滑动窗口 CacheHelper.SetCache(userid, LoginUser, DateTime.Now.AddMinutes(20)); } else { filterContext.HttpContext.Response.Redirect("/Login/Index"); } } if (LoginUser.UName == "wangzhen") { return; } else { string url = filterContext.HttpContext.Request.Url.AbsolutePath; string httpmethod = filterContext.HttpContext.Request.HttpMethod.ToLower(); //与当前登录的用户的权限进行对比 IApplicationContext ctx = ContextRegistry.GetContext(); IActionInfoService ActionInfoService = ctx.GetObject("ActionInfoService") as IActionInfoService; IR_UserInfo_ActionInfoService UAInfoService = ctx.GetObject("R_UserInfo_ActionInfoService") as IR_UserInfo_ActionInfoService; var action = ActionInfoService.GetEntities(a => a.Url.ToLower() == url && a.HttpMethod.ToLower() == httpmethod).FirstOrDefault(); if (action == null) { filterContext.HttpContext.Response.Redirect("/Error.html"); } //特殊权限校验 var rUAs = UAInfoService.GetEntities(u => u.UserInfoID == LoginUser.ID); var item = (from a in rUAs where a.ActionInfoID == action.ID select a).FirstOrDefault(); if (item != null) { if (item.IsPass == true) { return; } else { filterContext.HttpContext.Response.Redirect("/Error.html"); } } } } }
protected override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); bool isExt = false; // if (Session["userInfo"] == null) if (Request.Cookies["sessionId"] != null) { string sessionId = Request.Cookies["sessionId"].Value; //接收从Cookie中传递过来的Memcache的key object obj = Common.MemcacheHelper.Get(sessionId); //根据key从Memcache中获取用户的信息 if (obj != null) { UserInfo userInfo = Common.SerializerHelper.DeserializeToObject <UserInfo>(obj.ToString()); LoginUser = userInfo; isExt = true; //Common.MemcacheHelper.Set(sessionId, obj.ToString(), DateTime.Now.AddMinutes(20));//模拟滑动过期时间 #region 完成权限过滤 if (LoginUser.UName == "张廷宇") { return; } if (LoginUser.UName == "admin") { return; } string actionurl = Request.Url.AbsolutePath.ToLower(); //请求地址 string actionhttpmethod = Request.HttpMethod; //请求方式 //if (actionurl == "/home/index") //{ // return; //} IApplicationContext ctx = ContextRegistry.GetContext(); IUserInfoService UserInfoservice = (IUserInfoService)ctx.GetObject("UserInfoService"); IActionInfoService ActionInfoService = (IActionInfoService)ctx.GetObject("ActionInfoService"); //var url2 = ActionInfoService.LoadEntities(x => x.ID > 0); //var url = ActionInfoService.LoadEntities(a => a.Url == actionurl).FirstOrDefault(); // var url1 = ActionInfoService.LoadEntities(a => a.HttpMethod == actionhttpmethod).FirstOrDefault(); var actioninfo = ActionInfoService.LoadEntities(a => a.Url == actionurl && a.HttpMethod == actionhttpmethod).FirstOrDefault(); if (actioninfo == null) { //在权限表中没有找到要查询的URI方法 或者 请求方式错误 Response.Redirect("/Error.html"); return; } else { //判断登陆用户是否有权限访问 //按照第二条进行判断 var loginuserInfo = UserInfoservice.LoadEntities(u => u.ID == LoginUser.ID).FirstOrDefault(); var r_userinfo_actioninfo = (from a in loginuserInfo.R_UserInfo_ActionInfo where a.ActionInfoID == actioninfo.ID select a).FirstOrDefault(); if (r_userinfo_actioninfo != null) { if (r_userinfo_actioninfo.IsPass == true) { return; } else { Response.Redirect("/Error.html"); return; } } //安装第一条线进行过滤(用户——角色——权限) var loginUserRoleInfo = loginuserInfo.RoleInfoes; var loginuserisAction = (from r in loginUserRoleInfo from a in r.ActionInfoes where a.ID == actioninfo.ID select a).Count(); if (loginuserisAction < 1) { Response.Cookies["cp1"].Expires = DateTime.Now.AddDays(-1); Response.Cookies["cp2"].Expires = DateTime.Now.AddDays(-1); Response.Redirect("/Qxerrer.html"); return; } } #endregion } else { filterContext.HttpContext.Response.Redirect("/Login/Index"); return; } } if (!isExt) { filterContext.HttpContext.Response.Redirect("/Login/Index"); return; } }
public ActionInfoController(IActionInfoService _actionInfoService, IRoleInfoService _roleInfoService) { actionInfoService = _actionInfoService; roleInfoService = _roleInfoService; }
protected override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); #region 测试:去掉登录验证 //return; #endregion if (IsCheckUserLogin) { #region 用户登录校验 //校验用户是否已经登录 //if (filterContext.HttpContext.Session["loginUser"] == null) //{ // filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); //} //else //{ // LoginUser = filterContext.HttpContext.Session["loginUser"] as UserInfo; //} //使用memcache+cookie代替session //从缓存中拿到当前登录的用户信息 if (Request.Cookies["userLoginId"] == null) { filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); return; } string userGuid = Request.Cookies["userLoginId"].Value; UserInfo userInfo = Common.Cache.CacheHelper.GetCache(userGuid) as UserInfo; if (userInfo == null) { //用户长时间不操作,超时了 filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); return; } LoginUser = userInfo; //滑动窗口机制 Common.Cache.CacheHelper.SetCache(userGuid, userInfo, DateTime.Now.AddMinutes(20)); #endregion #region 权限校验 if (LoginUser.UName == "wei" || LoginUser.UName == "张三" || LoginUser.UName == "李四") { return; //后门 } string url = Request.Url.AbsolutePath.ToLower(); string httpMethod = Request.HttpMethod.ToLower(); // 基类注入必须通过子类,这里先不用属性注入,用spring容器直接获取 IApplicationContext context = ContextRegistry.GetContext(); IActionInfoService actionInfoService = context.GetObject("ActionInfoService") as IActionInfoService; //直接通过容器 IR_UserInfo_ActionInfoService rUserInfoActionInfoService = context.GetObject("R_UserInfo_ActionInfoService") as IR_UserInfo_ActionInfoService; IUserInfoService userInfoService = context.GetObject("UserInfoService") as IUserInfoService; //拿到当前请求的权限数据 var actionInfo = actionInfoService.GetEntities(u => u.Url.ToLower() == url && u.HttpMethod.ToLower() == httpMethod && u.DelFlag == this.delFlagNormal).FirstOrDefault(); if (actionInfo == null) { Response.Redirect("/Error.html"); } //拿到当前用户的特殊权限,然后看一下是否包括上述请求权限 var rUAs = rUserInfoActionInfoService.GetEntities( u => u.UserInfoId == LoginUser.Id && u.DelFlag == this.delFlagNormal); var item = (from r in rUAs where r.ActionInfoId == actionInfo.Id && r.DelFlag == this.delFlagNormal select r).FirstOrDefault(); if (item != null) { if (item.HasPermission == true) { return; //说明有这个权限,放行 } else { Response.Redirect("/Error.html"); //说明限制了这个权限,直接到错误页 } } //拿到当前用户的普通权限 var user = userInfoService.GetEntities(u => u.Id == LoginUser.Id && u.DelFlag == this.delFlagNormal).FirstOrDefault(); var allRoles = from r in user.RoleInfo where r.DelFlag == this.delFlagNormal select r; var actions = from r in allRoles from a in r.ActionInfo where a.DelFlag == this.delFlagNormal select a; var temp = (from a in actions where a.Id == actionInfo.Id select a).Count(); if (temp <= 0) { Response.Redirect("/Error.html"); //说明没有这个权限 } #endregion } }
public RoleInfoController(IRoleInfoService rs, IActionInfoService ias) { this.roleInfoService = rs; this.actionInfoService = ias; }