public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (context.HttpContext.User.Identity.Name == null)
            {
                context.Result = new RedirectToRouteResult("default", new { controller = "System", action = "ManageLogin" });
                base.OnActionExecuting(context);
                return;
            }
            var userId = UserIdToUserNameDictionary.GetUserId(context.HttpContext.User.Identity.Name);
            var user   = context.HttpContext.Session.Get <User>(userId.ToString());

            if (!user.IsAllowEntryManager())
            {
                context.Result = new RedirectToRouteResult("Error", new { controller = "Error", action = "SystemError", errorMsg = "您目前没有权限登录管理后台" });
            }
            base.OnActionExecuting(context);
        }
Esempio n. 2
0
        /// <summary>
        /// 验证用户名和密码是否匹配
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="passWord">密码</param>
        /// <returns>登录状态枚举</returns>
        public UserLoginStatus ValidateUser(string userName, string passWord)
        {
            long userId = UserIdToUserNameDictionary.GetUserId(userName);
            User user   = userRepository.GetUser(userId);

            if (user == null)
            {
                return(UserLoginStatus.InvalidCredentials);
            }
            if (!userName.Equals(user.UserName, StringComparison.CurrentCulture))
            {
                return(UserLoginStatus.InvalidCredentials);
            }
            if (!UserPasswordHelper.CheckPassword(passWord, user.Password, (UserPasswordFormat)user.PasswordFormat))
            {
                return(UserLoginStatus.InvalidCredentials);
            }
            if (!user.IsActivated)
            {
                return(UserLoginStatus.NotActivated);
            }
            if (user.IsBanned)
            {
                if (user.BanDeadLine >= DateTime.UtcNow)
                {
                    return(UserLoginStatus.Banned);
                }
                else
                {
                    user.IsBanned    = false;
                    user.BanDeadLine = DateTime.UtcNow;
                    userRepository.Update(user);
                }
            }

            return(UserLoginStatus.Success);
        }
        /// <summary>
        /// 根据用户名查询器获取用户实体
        /// </summary>
        /// <param name="userService"></param>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        public static User GetFullUser(this IUserService userService, string userName)
        {
            long userId = UserIdToUserNameDictionary.GetUserId(userName);

            return(userService.GetUserRepository().GetUser(userId));
        }