Beispiel #1
0
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            string controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            string action     = filterContext.ActionDescriptor.ActionName;
            string actionPath = "/" + controller + "/" + action;

            _logger.Info("菜单高亮:" + actionPath + ";访问IP:" + CerCommon.GetIp() + "; 登录账号:" + filterContext.HttpContext.User.Identity.Name);

            filterContext.Controller.ViewData["CurrentMenu"]       = CurrentItem;
            filterContext.Controller.ViewData["CurrentParentItem"] = CurrentParentItem;
            var    cache = filterContext.HttpContext.Cache;
            string key   = string.Format("rolefunction_{0}", ContextService.Current.GetCookieValue("role"));

            if (cache[key] == null)
            {
                Guid rs;
                if (!Guid.TryParse(ContextService.Current.GetCookieValue("role"), out rs))
                {
                    var user = ServiceActivator.Get <UserService>().GetByUserName(filterContext.HttpContext.User.Identity.Name);
                    rs = user.RoleId;
                    ContextService.Current.SetCookie("role", user.RoleId.ToString());
                }
                var menus = ServiceActivator.Get <RoleFunctionService>().Get(rs);
                cache.Add(key, menus, new SqlCacheDependency("MonkeyCacheDependency", "BASE_ROLEFUNCTIONS"), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
            }
            //ContextService.Current.Cache("");

            filterContext.Controller.ViewData["Menus"] = cache[key];
            base.OnActionExecuted(filterContext);
        }
Beispiel #2
0
        //
        // GET: /Account/Login
        //[AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            _logger.Info("登录页面:访问IP:" + CerCommon.GetIp());

            if (CurrentUser != null && !(CurrentUser is EmptyUserContract))
            {
                _formsAuthentication.SetAuthCookie(CurrentUser.UserName, false);
                _contextService.SetCookie("role", CurrentUser.RoleId.ToString());
                _contextService.NickName  = CurrentUser.NickName;
                _contextService.DepId     = CurrentUser.DepId.ToString();
                _contextService.UserPhoto = ConfigurationManager.AppSettings["USER_AVATAR"] + CurrentUser.UserInfoPhoto;
                _logger.Info(CurrentUser.Id + "登录成功" + "文档管理系统");

                return(Redirect("/home/index"));
            }
            var model = new LoginModel();
            //读取保存的Cookie信息
            HttpCookie cookies = Request.Cookies["USER_COOKIE"];

            if (cookies != null && !string.IsNullOrEmpty(cookies.Value))
            {
                //如果Cookie不为空,则将Cookie里面的用户名和密码读取出来赋值给前台的文本框。
                model.UserName = Md5Util.Decrypt(cookies["UserName"]);
                model.Password = Md5Util.Decrypt(cookies["UserPassword"]);

                //这里依然把记住密码的选项给选中。
                model.RememberMe  = true;
                ViewBag.ReturnUrl = returnUrl;
                if (model.AutoLogin)
                {
                    return(Login(model, returnUrl));
                }

                return(View(model));
            }

            //if (!string.IsNullOrEmpty(returnUrl) && returnUrl.EndsWith("/account/logoff"))
            //{
            //    returnUrl = returnUrl.Replace("/account/logoff", "/home/index");
            //}
            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            var filters = filterContext.ActionDescriptor.GetCustomAttributes(false);

            if (filters.Any(p => p is AllowAnonymousAttribute))
            {
                return;
            }
            string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            string actionName     = filterContext.ActionDescriptor.ActionName;
            string actionPath     = "/" + controllerName + "/" + actionName;
            // filterContext.Result = new RedirectResult("/Account/AuthorError");
            var identity = filterContext.HttpContext.User.Identity;

            if (identity.IsAuthenticated)
            {
                var user = ServiceActivator.Kernel.Get <UserService>().GetByUserName(identity.Name);

                _logger.Info("权限判断:" + actionPath + ";访问IP:" + CerCommon.GetIp() + "; 登录账号:" + user.UserName);

                if (user != null)
                {
                    if (filterContext.HttpContext.Session != null && string.IsNullOrEmpty(ContextService.Current.NickName))
                    {
                        ContextService.Current.NickName = user.NickName;
                    }
                    var controller = filterContext.HttpContext.Request.RequestContext.RouteData.Values["controller"];
                    var action     = filterContext.HttpContext.Request.RequestContext.RouteData.Values["action"];
                    if (controller != null && action != null)
                    {
                        //if (controller.ToString().ToLower() == "home") return ;
                        string filePath = "/" + controller + "/" + action;
                        string key      = string.Format("rolefunctionView_{0}", user.RoleId);
                        var    cache    = filterContext.HttpContext.Cache;
                        if (cache[key] == null)
                        {
                            Guid id;
                            if (!Guid.TryParse(ContextService.Current.GetCookieValue("role"), out id))
                            {
                                id = user.RoleId;
                                ContextService.Current.SetCookie("role", user.RoleId.ToString());
                            }
                            var menus = ServiceActivator.Kernel.Get <RoleFunctionService>().GetByRole(id);
                            cache.Add(key, menus, new SqlCacheDependency("MonkeyCacheDependency", "BASE_ROLEFUNCTIONS"),
                                      Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
                                      CacheItemPriority.Normal,
                                      null);
                        }
                        var menusContract = cache[key] as List <MenuItemContract>;
                        if (menusContract != null)
                        {
                            try
                            {
                                var rs = menusContract.Exists(p =>
                                                              String.Compare(p.Action.ToLower(), filePath.ToLower(),
                                                                             StringComparison.OrdinalIgnoreCase) == 0);
                                _logger.Info("权限判断:" + filePath + ";是否有权限:" + rs + "; 登录账号:" + user.UserName);
                                if (!rs)
                                {
                                    //  权限默认页跳转
                                    if (filePath.ToLower().Contains("home/index") && menusContract.Count > 0)
                                    {
                                        var roleList =
                                            menusContract.Where(p => p.Action != "/" && !string.IsNullOrWhiteSpace(p.Action))
                                            .OrderBy(p => p.OrderBy).ToList();
                                        string firstAction = roleList[0].Action;
                                        filterContext.Result = new RedirectResult(firstAction);
                                    }
                                    else
                                    {
                                        filterContext.Result = new RedirectResult("/Account/AuthorError");
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                _logger.Error("权限判断:" + actionPath + ";访问IP:" + CerCommon.GetIp() + "; 登录账号:" + user.UserName, e);
                            }
                        }
                        else
                        {
                            filterContext.Result = new RedirectResult("/Account/AuthorError");
                        }
                    }
                }
            }
            base.OnAuthorization(filterContext);
        }