//权限判断业务逻辑 protected virtual bool AuthorizeCore(ActionExecutingContext filterContext, bool isViewPage) { if (filterContext.HttpContext == null) { throw new ArgumentNullException("httpContext"); } if (!filterContext.HttpContext.User.Identity.IsAuthenticated) { return false;//判定用户是否登录 } //获取当前用户信息 UserInfo user = new UserInfo(); UserInfoService _UserInfoService = new UserInfoService(); user = _UserInfoService.GetKey((filterContext.HttpContext.User.Identity as FormsIdentity).Ticket.UserData.Split("|".ToCharArray())[0]); //var area = filterContext.RouteData.DataTokens["area"]; //var controllerName = filterContext.RouteData.Values["controller"].ToString(); //var actionName = filterContext.RouteData.Values["action"].ToString(); if (isViewPage)//如果当前Action请求为具体的功能页并且不是MasterPage页 { Predicate<SystemMenu> match = delegate(SystemMenu menu) { if ((menu.Code.ToLower()) == area + "." + Controller + "." + Action) return true; else return false; }; SystemMenu has_menu = user.Role.Menu.Find(match); if (has_menu == null) return false; //if (user.Role.Menu(m => m.ControllerName == controllerName && m.ActionName == actionName) == 0) // return false; } else { //var actions = ContainerFactory.GetContainer().Resolve<IAuthorityFacade>().GetAllActionPermission();//所有被维护的Action权限 //if (actions.Count(a => a.ControllerName == controllerName && a.ActionName == actionName) != 0)//如果当前Action属于被维护的Action权限 //{ // if (user.ActionPermission.Count(a => a.ControllerName == controllerName && a.ActionName == actionName) == 0) // return false; //} } return true; }
/// <summary> /// 设置登录信息COOKIE /// </summary> /// <param name="model">页面登录账户实体</param> /// <param name="userObject">访问数据库实体</param> private void SetCookie(AccountModels model, UserInfo userObject) { //获取登录COOKIE HttpCookie cookie = FormsAuthentication.GetAuthCookie(model.UserName, false); if (userObject != null) { cookie = FormsAuthentication.GetAuthCookie(userObject.LoginName, false); } //cookie 加密 登录信息标识 FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value); //新建 配置 登录信息标识 FormsAuthenticationTicket newTicket = null; if (userObject != null) { newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, string.Format("{0}|{1}|{2}|{3}", userObject.Id, userObject.NickName, userObject.Email, string.Join(",", userObject.Role.Menu.Select(m => m.Code).ToArray()))); //创建加密COOKIE cookie.Value = FormsAuthentication.Encrypt(newTicket); //FormsAuthentication.SetAuthCookie(model.UserName, false); //添加浏览器COOKIE base.Response.Cookies.Add(cookie); } else { newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, "0|超级管理员|" + System.Configuration.ConfigurationManager.AppSettings["NetName"].Trim().ToString() + "@admin.com" + "| "); //创建加密COOKIE cookie.Value = FormsAuthentication.Encrypt(newTicket); //FormsAuthentication.SetAuthCookie(model.UserName, false); //添加浏览器COOKIE base.Response.Cookies.Add(cookie); } }