Example #1
0
        public IActionResult Login(UserLoginCommand command)
        {
            if (null == command)
            {
                return(Json(new { code = -1, msg = "Error:数据不正确", url = string.Empty }));
            }
            else if (command.Account.IsNullOrWhitespace() || command.Passwrd.IsNullOrWhitespace())
            {
                return(Json(new { code = -1, msg = "Error:数据不正确", url = string.Empty }));
            }
            else if (command.VerifyCode != HttpContext.Session.GetString("verifycode"))
            {
                HttpContext.Session.Remove("verifycode");
                return(Json(new { code = -1, msg = "Error:验证码错误", url = string.Empty }));
            }



            var commandResult = this._commandInvokerFactory.Handle <UserLoginCommand, UserLoginCommandResult>(command);


            if (!commandResult.IsSuccess)
            {
                return(Json(new { code = -1, msg = $"错误:{commandResult.GetErrors()[0]}", url = string.Empty }));
            }

            // 登陆验证成功
            HttpContext.Session.Remove("verifycode");
            AccountLoginManager.SetLogin(HttpContext, commandResult);


            return(Json(new { code = 1, msg = "Success:登陆成功", url = command.ReturnUrl.IsNullOrWhitespace() ? "/Admin/Index" : command.ReturnUrl }));
        }
        // 在Action执行之前检查是否登陆
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            // 判断是否已登录
            if (!AccountLoginManager.CheckLogin(context.HttpContext))
            {
                // 清除登陆痕迹
                AccountLoginManager.SetLoginOut(context.HttpContext);

                // 如果为Post请求
                if (context.HttpContext.Request.Method.Equals("post", StringComparison.CurrentCultureIgnoreCase))
                {
                    // 如果请求的控制器为AdminPostController
                    if (context.HttpContext.Request.Path.Value.StartsWith("/AdminPost"))
                    {
                        context.Result = new JsonResult(new { code = "-2", msg = "Error:登陆过期", url = "/Account/Index" });
                    }
                    // 如果请求的控制器为FileController
                    else if (context.HttpContext.Request.Path.Value.StartsWith("/File"))
                    {
                        context.Result = new ContentResult()
                        {
                            Content = "/Contents/Posts/UpLoadImgs/expiration.png"
                        }
                    }
                    ;
                }

                // 其他
                context.Result = new RedirectResult($"/Account/Index?ReturnUrl={context.HttpContext.Request.Path}");
            }
            base.OnActionExecuting(context);
        }
    }
        public IActionResult ChangePassword(ChangePasswordCommand command)
        {
            // 过滤请求数据
            if (null == command)
            {
                return(Json(new { code = -1, msg = "Error:数据不正确", url = string.Empty }));
            }
            else if (command.NewPassword.IsNullOrWhitespace() || command.ConfirmNewPassword.IsNullOrWhitespace() || command.OldPassword.IsNullOrWhitespace())
            {
                return(Json(new { code = -1, msg = "Error:数据不正确", url = string.Empty }));
            }
            else if (command.NewPassword != command.ConfirmNewPassword)
            {
                return(Json(new { code = -1, msg = "Error:两次输入密码不一致", url = string.Empty }));
            }

            // 得到当前登录的账号
            command.UserAccount = HttpContext.User.FindFirst(ClaimTypes.Sid).Value;

            // 执行修改密码命令
            var commandResult = this._commandInvokerFactory.Handle <ChangePasswordCommand, CommandResult>(command);

            // 执行发生错误
            if (!commandResult.IsSuccess)
            {
                return(Json(new { code = -1, msg = $"Error:{commandResult.GetErrors()[0]}", url = string.Empty }));
            }

            // 执行成功,清除登陆痕迹返回结果
            AccountLoginManager.SetLoginOut(HttpContext);
            return(Json(new { code = 1, msg = "Success:修改成功", url = "/Account/Index" }));
        }
Example #4
0
        public IActionResult Register(UserRegisterCommand command)
        {
            if (null == command)
            {
                return(Json(new { code = -1, msg = "Error:数据不正确", url = string.Empty }));
            }
            else if (command.UserAccount.IsNullOrWhitespace())
            {
                return(Json(new { code = -1, msg = "Error:数据不正确", url = string.Empty }));
            }
            else if (command.Password.IsNullOrWhitespace())
            {
                return(Json(new { code = -1, msg = "Error:数据不正确", url = string.Empty }));
            }
            else if (command.ConfirmPassword.IsNullOrWhitespace())
            {
                return(Json(new { code = -1, msg = "Error:数据不正确", url = string.Empty }));
            }
            else if (command.Password != command.ConfirmPassword)
            {
                return(Json(new { code = -1, msg = "Error:数据不正确", url = string.Empty }));
            }
            else if (this.CheckAccount(this._commandInvokerFactory))
            {
                return(Json(new { code = 1, msg = "Error:已存在后台管理账号", url = "/Account/Index" }));
            }


            var commandResult = this._commandInvokerFactory.Handle <UserRegisterCommand, UserLoginCommandResult>(command);


            if (!commandResult.IsSuccess)
            {
                return(Json(new { code = -1, msg = $"错误:{commandResult.GetErrors()[0]}", url = string.Empty }));
            }


            AccountLoginManager.SetLogin(HttpContext, commandResult);
            return(Json(new { code = 1, msg = "Success:注册成功", url = "/Admin/Index" }));
        }
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            // 如果已经登陆则跳转到后台管理页面
            if (AccountLoginManager.CheckLogin(context.HttpContext))
            {
                switch (context.HttpContext.Request.Method.ToLower())
                {
                case "get":
                    context.Result = new RedirectResult("/Admin/Index");
                    break;

                case "post":
                    context.Result = new JsonResult(new { code = -1, msg = "Error:已登录", url = "/Admin/Index" });
                    break;
                }
            }// 如果没有登陆则清除登陆痕迹
            else
            {
                AccountLoginManager.SetLoginOut(context.HttpContext);
            }
            base.OnActionExecuting(context);
        }
 public IActionResult LoginOut()
 {
     AccountLoginManager.SetLoginOut(HttpContext);
     return(RedirectToAction("Index", "Account"));
 }