コード例 #1
0
        public ActionResult Index(int logInDefault = 0)
        {
            //获取该用户拥有的相关模块
            var cookie = Request.Cookies.Get(TianYuConsts.SystemLoginCookieName);

            var menuViewModel = CacheHelper.Get <IEnumerable <SystemMenuRoleViewModel> >(TianYuConsts.GetLoginUserMenuCacheKey(cookie.Value));
            //读取一级菜单
            var oneLevelMenu = menuViewModel.Where(x => x.Level == 1).OrderBy(x => x.MenuSort).ToList();

            ViewBag.OneLevelMenu = oneLevelMenu;

            if (logInDefault == 0 && oneLevelMenu != null)
            {
                logInDefault = oneLevelMenu.FirstOrDefault().Id;
            }
            ViewBag.Refres = logInDefault;

            //读取一级以下菜单
            ViewBag.OtherLevelMenuHtml = GetSubMenuHtml(menuViewModel.ToList(), logInDefault);

            //ViewBag.LogoutUrl = ConfigHelper.GetAppsettingValue(TianYuConsts.SystemManagerLogoutDomain + "/Home/Login");

            var loginInfo = CacheHelper.Get <SystemLoginUserInfo>(TianYuConsts.GetLoginUserInfoCacheKey(cookie.Value));

            return(View(loginInfo));
        }
コード例 #2
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.ActionDescriptor.GetCustomAttributes(typeof(MvcIgnoreLoginAttribute), true).Any() || filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(MvcIgnoreLoginAttribute), true).Any())
            {
                base.OnActionExecuting(filterContext);
                return;
            }
            //未登录时,跳转到登录
            var cookie = CookieHelper.GetCookieValue(TianYuConsts.SystemLoginCookieName);

            var loginInfoCacheKey = TianYuConsts.GetLoginUserInfoCacheKey(cookie);

            var loginInfo = CacheHelper.Get <SystemLoginUserInfo>(loginInfoCacheKey);

            if (loginInfo == null)
            {
                var loginUrl = "/Home/Login";

                var request = filterContext.RequestContext.HttpContext.Request;
                //取消回调地址,因使用首页框架内嵌页面,此处回调只能回调首页
                //var returnUrl = request.Url.OriginalString;
                var returnUrl = string.Format("{0}://{1}/Home/Index", request.Url.Scheme, request.Url.Authority);

                var redirect = new RedirectResult(string.Format("{0}?redirectUrl={1}", loginUrl,
                                                                WebUtility.UrlEncode(returnUrl)));
                filterContext.Result = redirect;
                return;
            }

            base.OnActionExecuting(filterContext);
            return;
        }
コード例 #3
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.ActionDescriptor.GetCustomAttributes(typeof(MvcIgnorePowerAttribute), true).Any() || filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(MvcIgnorePowerAttribute), true).Any())
            {
                base.OnActionExecuting(filterContext);
                return;
            }
            string areaName       = filterContext.RouteData.DataTokens["area"].IsNull() ? "" : filterContext.RouteData.DataTokens["area"].ToString();
            string controllerName = filterContext.RouteData.Values["controller"].ToString();
            string actionName     = filterContext.RouteData.Values["action"].ToString();
            var    actionUrl      = string.Format("{0}/{1}/{2}", areaName, controllerName, actionName);
            ////未登录时,跳转到登录
            var currCookie = CookieHelper.GetCookieValue(TianYuConsts.SystemLoginCookieName);

            var loginInfoCacheKey = TianYuConsts.GetLoginUserInfoCacheKey(currCookie);


            //if (token.IsNullOrWhiteSpace())
            //{
            //    var loginUrl = ConfigHelper.GetAppsettingValue(TianYuConsts.SystemManagerLoginUrl);
            //    var request = filterContext.RequestContext.HttpContext.Request;
            //    var redirect = new RedirectResult(string.Format("{0}?redirectUrl={1}", loginUrl,
            //        WebUtility.UrlEncode(request.Url.OriginalString)));
            //    filterContext.Result = redirect;
            //    return;
            //}
            var loginInfo = CacheHelper.Get <SystemLoginUserInfo>(loginInfoCacheKey);

            //if (userGroup == null)
            //{
            //    var loginUrl = ConfigHelper.GetAppsettingValue(TianYuConsts.SystemManagerLoginUrl);
            //    var request = filterContext.RequestContext.HttpContext.Request;
            //    var redirect = new RedirectResult(string.Format("{0}?redirectUrl={1}", loginUrl,
            //        WebUtility.UrlEncode(request.Url.OriginalString)));
            //    filterContext.Result = redirect;
            //    return;
            //}
            if (loginInfo == null)
            {
                throw new Exception("没有找到登录用户信息");
            }

            //延长cookie时间
            //      CookieHelper.AppendCookieTime(TianYuConsts.SystemLoginCookieName, 24);

            var cookie = HttpContext.Current.Request.Cookies[TianYuConsts.SystemLoginCookieName];

            cookie.Domain   = ConfigHelper.GetAppsettingValue("CookieDomainName");
            cookie.Expires  = DateTime.Now.AddHours(24);
            cookie.HttpOnly = false;
            cookie.Path     = "/";
            filterContext.HttpContext.Response.Cookies.Add(cookie);

            if (IsPowerAction(actionUrl, loginInfo))
            {
                base.OnActionExecuting(filterContext);
            }
            else
            {
                var jsonResult = new JsonResult();
                var result     = new BaseResponse()
                {
                    Status       = HttpStatusCode.Unauthorized,
                    ErrorMessage = "您没有访问权限"
                };
                jsonResult.Data = result;
                if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
                {
                    filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;

                    jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                    filterContext.Result           = jsonResult;
                }
                else
                {
                    var redirect = new RedirectResult("/Error/index?msg=" + result.ErrorMessage);
                    filterContext.Result = redirect;
                }


                return;
            }
        }
コード例 #4
0
        /// <summary>
        /// 登录后台
        /// </summary>
        /// <param name="loginName">用户名</param>
        /// <param name="loginPwd">密码</param>
        /// <returns>登录凭据</returns>
        public BusinessBaseViewModel <string> Login(string loginName, string loginPwd)
        {
            var response = new BusinessBaseViewModel <string>()
            {
                Status = ResponseStatus.Fail
            };

            if (loginName.IsNullOrWhiteSpace() || loginPwd.IsNullOrWhiteSpace())
            {
                response.ErrorMessage = "请输入用户名或密码";
                return(response);
            }

            var Staff = _staffRepostory.FirstOrDefault(t => t.LoginName == loginName && t.Status != (int)SystemStaffStatus.Del);

            if (Staff.IsNull())
            {
                response.ErrorMessage = "请输入用户名不存在或密码错误";
                return(response);
            }

            if (Staff.Status == (int)SystemStaffStatus.Stop)
            {
                response.ErrorMessage = "该用户已经被禁用";
                return(response);
            }

            if (Staff.LoginPwd.Equals((loginPwd + Staff.MaskCode).ToMd5(), StringComparison.InvariantCultureIgnoreCase))
            {
                //更新最近登录时间
                Staff.LastLoginTime = DateTime.Now;
                _staffRepostory.Update(Staff, "LastLoginTime");
                _staffRepostory.SaveChanges();

                //生成一个登录凭据
                var sessionIdString = $"admin:login:{loginName}:{Utils.NewGuid()}";
                var sessionId       = DESEncrypt.Encrypt(sessionIdString.ToBase64());

                string sessionKey = TianYuConsts.GetSessionIdCacheKey(sessionId);
                if (CacheHelper.Exists(sessionKey))
                {
                    CacheHelper.Remove(sessionKey);
                }
                //将用户菜单权限缓存到cache
                var menuList = _systemRoleService.FindStaffMenuRole(Staff.Id);

                CacheHelper.Insert(TianYuConsts.GetLoginUserMenuCacheKey(sessionId), menuList, true);

                var buttonList = _systemRoleService.FindStaffRoleNameByStaffId(Staff.Id);

                CacheHelper.Insert(TianYuConsts.GetLoginUserButtonCacheKey(sessionId), buttonList, true);

                var loginUserInfo = new SystemLoginUserInfo
                {
                    Id        = Staff.Id,
                    Eamil     = Staff.Eamil,
                    LoginName = Staff.LoginName,
                    Mobile    = Staff.Mobile,
                    NickName  = Staff.NickName,
                    SectionId = Staff.SectionId,
                    Status    = Staff.Status,
                    Tel       = Staff.Tel
                };
                //存储当前登录用户数据
                CacheHelper.Insert(TianYuConsts.GetLoginUserInfoCacheKey(sessionId), loginUserInfo, DateTime.Now.AddHours(1));

                response.BusinessData = sessionId;
                response.Status       = ResponseStatus.Success;
                return(response);
            }
            else
            {
                response.ErrorMessage = "请输入用户名不存在或密码错误";
                return(response);
            }
        }