Ejemplo n.º 1
0
        public IActionResult TestLogin()
        {
            try
            {
                using EFCoreContextWrite context = new EFCore.EFCoreContextWrite();

                UserInfo userInfo = new UserInfo()
                {
                    id       = 1,
                    Email    = "Test",
                    AuthRole = new List <AuthRole>()
                    {
                        AuthRole.User
                    }
                };
                string token = Guid.NewGuid().ToString();
                AuthRedis.SetToken(userInfo, token, LoginType.LimitWeb);
                return(Ok(new ApiResponse(new { token })));
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                throw ex;
            }
        }
        public IActionResult AuthLogin(LoginDto body)
        {
            var User = _BaseService.GetListWriteBy <Users>(x => x.UserName == body.UserName);

            if (User.Count <= 0)
            {
                return(Ok(new ApiNResponse(code: CodeAndMessage.用户名不存在, message: "The user name does not exist")));
            }
            if (User.Where(x => x.UserName == body.UserName && x.PassWord == HashPass.HashString(body.PassWord, "MD5")).Count() <= 0)
            {
                return(Ok(new ApiNResponse(code: CodeAndMessage.密码错误, message: "Password error")));
            }

            if (User.Where(x => x.UserName == body.UserName && x.PassWord == HashPass.HashString(body.PassWord, "MD5") && x.CreateTime.AddHours(2) < DateTime.Now && x.LoginType == LoginType.LimitWeb).Count() > 0)
            {
                return(Ok(new ApiNResponse(code: CodeAndMessage.注册时间已经超过2小时, message: "The registration time has exceeded 2 hours. Please re-register")));
            }

            UserInfo userInfo = new UserInfo();

            foreach (var item in User)
            {
                userInfo = new UserInfo()
                {
                    id       = item.Id,
                    AuthRole = new List <AuthRole>()
                    {
                        item.AuthRole
                    },
                    Email     = item.Email,
                    LoginType = new List <LoginType>()
                    {
                        item.LoginType
                    },
                    CreateTime = item.CreateTime
                };
            }
            string   token     = Guid.NewGuid().ToString();
            AuthRole AuthRoles = userInfo.AuthRole.First();

            switch (AuthRoles)
            {
            case Models.AuthRole.Admin:
                AuthRedis.GetUserById(userInfo.id, LoginType.FreeWeb);
                AuthRedis.SetToken(userInfo, token, LoginType.FreeWeb);
                break;

            case Models.AuthRole.User:
                AuthRedis.GetUserById(userInfo.id, LoginType.LimitWeb);
                AuthRedis.SetToken(userInfo, token, LoginType.LimitWeb);
                break;

            default:
                break;
            }
            return(Ok(new ApiResponse(new { token, AuthRoles })));
        }