public IActionResult Index()
        {
            string s = EncryptorHelper.GetMd5(Guid.NewGuid().ToString());

            HttpContext.Session.SetString(R_KEY, s);
            //HttpContext.Response.Cookies.Append(R_KEY,s);
            LoginModel loginModel = new LoginModel()
            {
                Range = s
            };

            return(View(loginModel));
        }
        public IActionResult Regiter()
        {
            var salt     = Guid.NewGuid().ToString();
            var password = EncryptorHelper.GetMd5("123456" + salt);
            var data     = _sysUserService.Add(new SysUser()
            {
                Account        = "xiehanbing",
                Password       = password,
                UserGuid       = Guid.NewGuid(),
                LastLoginTime  = DateTime.Now,
                Name           = "谢汉冰",
                Salt           = salt,
                LoginFailedNum = 0,
                LoginLock      = false,
            });

            if (data)
            {
                AjaxData.Status = true;
                return(Json(AjaxData));
            }
            return(Json(AjaxData));
        }
Пример #3
0
        /// <summary>
        /// <see cref="ISysUserService.ValidateUser(string,string,string)"/>
        /// </summary>
        public (bool success, string message, string token, Entity.User.SysUser user) ValidateUser(string account, string password, string r)
        {
            var user = GetByAccount(account);

            if (user == null)
            {
                return(false, "用户名或密码错误", null, null);
            }
            if (user.Enable)
            {
                return(false, "账号已被冻结", null, null);
            }
            if (user.LoginLock)
            {
                if (user.AllowLoginTime != null && user.AllowLoginTime > DateTime.Now)
                {
                    return(false, "账号已被锁定" + ((user.AllowLoginTime.Value - DateTime.Now).TotalMinutes + 1).ToString("F0") + "分钟", null, null);
                }
            }
            var md5password = EncryptorHelper.GetMd5(user.Password + r);

            if (password.Equals(md5password, StringComparison.InvariantCultureIgnoreCase))
            {
                user.LoginLock      = false;
                user.LoginFailedNum = 0;
                user.AllowLoginTime = null;
                user.Enable         = false;
                user.LastLoginTime  = DateTime.Now;
                //登录日志记录
                user.SysUserLoginLogs.Add(new SysUserLoginLog()
                {
                    Id        = Guid.NewGuid(),
                    IpAddress = "",
                    LoginTime = DateTime.Now,
                    Message   = "登录成功",
                    UserId    = user.UserGuid
                });
                //单点登录,移除旧的登录token

                var userToken = new SysUserToken()
                {
                    Id         = Guid.NewGuid(),
                    ExpireTime = DateTime.Now.AddHours(1),
                    SysUserId  = user.UserGuid
                };
                user.SysUserToken.Add(userToken);
                _repository.DbContext.SaveChanges();
                return(true, "登录成功", userToken.Id.ToString(), user);
            }
            //登录日志记录
            user.SysUserLoginLogs.Add(new SysUserLoginLog()
            {
                Id        = Guid.NewGuid(),
                IpAddress = "",
                LoginTime = DateTime.Now,
                Message   = "登录密码错误",
                UserId    = user.UserGuid
            });

            user.LoginFailedNum++;
            if (user.LoginFailedNum > 5)
            {
                user.LoginLock      = true;
                user.AllowLoginTime = DateTime.Now.AddHours(2);
            }
            _repository.DbContext.SaveChanges();
            return(false, "用户名或密码错误", null, null);
        }