Beispiel #1
0
        /// <summary>
        /// 自定义过滤器
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
        {
            string     cookieName = FormsAuthentication.FormsCookieName;
            HttpCookie authCookie = System.Web.HttpContext.Current.Request.Cookies[cookieName];
            FormsAuthenticationTicket authTicket = null;

            try
            {
                authTicket = FormsAuthentication.Decrypt(authCookie.Value);
            }
            catch (Exception ex)
            {
                return;
            }
            if (authTicket != null && filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                string UserId = authTicket.Name;
                if (!string.IsNullOrEmpty(UserId))
                {
                    var response = Implement.GetUserInfo(Convert.ToInt32(UserId));
                    mol = response.Data;
                    if (mol != null)
                    {
                        Uright            = mol.Role.RightVle;
                        ViewBag.MenusList = Implement.GetMenus(Uright);
                        ViewBag.UserName  = mol.Uname;
                        //把toke用户数据放到 HttpContext.Current.User 里
                        ClientUserData clientUserData = new ClientUserData()
                        {
                            UserId        = mol.Id,
                            Uname         = mol.Uname,
                            RoleId        = mol.RoleId,
                            pinyin        = mol.pinyin,
                            idCard        = mol.idCard,
                            PhotoNum      = mol.PhotoNum,
                            EmployeeSex   = mol.EmployeeSex,
                            EmployeePhone = mol.EmployeePhone,
                            Age           = mol.Age,
                            Worker        = mol.Worker,
                            HomeAddress   = mol.HomeAddress,
                            RightVle      = mol.Role.RightVle,
                            RightName     = mol.Role.RightName,
                            isAdd         = mol.Role.isAdd,
                            isUpdate      = mol.Role.isUpdate,
                            isDelete      = mol.Role.isDelete,
                        };
                        if (System.Web.HttpContext.Current != null)
                        {
                            System.Web.HttpContext.Current.User = new UserPrincipal(clientUserData);
                        }
                    }
                    base.OnActionExecuting(filterContext);
                }
            }
        }