Ejemplo n.º 1
0
        public async Task <IResponseOutput> LoginAsync(AuthLoginParam param)
        {
            #region 验证码校验
            if (Configs.AppSettings.VarifyCode.Enable)
            {
                var verifyCodeKey = string.Format(CacheKey.VerifyCodeKey, param.VerifyCodeKey);
                var exists        = await BaseCache.ExistsAsync(verifyCodeKey);

                if (exists)
                {
                    var verifyCode = await BaseCache.GetAsync(verifyCodeKey);

                    if (string.IsNullOrEmpty(verifyCode))
                    {
                        return(ResponseOutput.NotOk("验证码已过期!"));
                    }
                    if (verifyCode.ToLower() != param.VerifyCode.ToLower())
                    {
                        return(ResponseOutput.NotOk("验证码输入有误!", 2));
                    }
                    await BaseCache.DelAsync(verifyCodeKey);
                }
                else
                {
                    return(ResponseOutput.NotOk("验证码已过期!", 1));
                }
            }
            #endregion

            var user = (await _userRepo.FindAsync(a => a.UserName == param.UserName));
            if (string.IsNullOrWhiteSpace(user?.Id))
            {
                return(ResponseOutput.NotOk("账号输入有误!", 3));
            }

            #region 解密
            if (param.PasswordKey.NotNull())
            {
                var passwordEncryptKey = string.Format(CacheKey.PassWordEncryptKey, param.PasswordKey);
                var existsPasswordKey  = await BaseCache.ExistsAsync(passwordEncryptKey);

                if (existsPasswordKey)
                {
                    var secretKey = await BaseCache.GetAsync(passwordEncryptKey);

                    if (secretKey.IsNull())
                    {
                        return(ResponseOutput.NotOk("解密失败!", 1));
                    }
                    param.Password = DesEncrypt.Decrypt(param.Password, secretKey);
                    await BaseCache.DelAsync(passwordEncryptKey);
                }
                else
                {
                    return(ResponseOutput.NotOk("解密失败!", 1));
                }
            }
            #endregion

            var password = MD5Encrypt.Encrypt32(param.Password);
            if (user.Password != password)
            {
                return(ResponseOutput.NotOk("密码输入有误!", 4));
            }

            var authLoginOutput = ObjectMapper.Map <AuthLoginOutput>(user);//user.MapTo<UserEntity, AuthLoginOutput>();

            return(ResponseOutput.Ok(authLoginOutput));
        }
Ejemplo n.º 2
0
        public async Task <IResponseOutput> LoginAsync(AuthLoginInput input)
        {
            #region 验证码校验
            var verifyCodeKey = string.Format(CacheKey.VerifyCodeKey, input.VerifyCodeKey);
            var exists        = await _cache.ExistsAsync(verifyCodeKey);

            if (exists)
            {
                var verifyCode = await _cache.GetAsync(verifyCodeKey);

                if (string.IsNullOrEmpty(verifyCode))
                {
                    return(ResponseOutput.NotOk("验证码已过期!", 1));
                }
                if (verifyCode.ToLower() != input.VerifyCode.ToLower())
                {
                    return(ResponseOutput.NotOk("验证码输入有误!", 2));
                }
                await _cache.DelAsync(verifyCodeKey);
            }
            else
            {
                return(ResponseOutput.NotOk("验证码已过期!", 1));
            }
            #endregion

            var user = (await _userRepository.GetAsync(a => a.UserName == input.UserName));
            if (!(user?.Id > 0))
            {
                return(ResponseOutput.NotOk("账号输入有误!", 3));
            }

            #region 解密
            if (input.PasswordKey.NotNull())
            {
                var passwordEncryptKey = string.Format(CacheKey.PassWordEncryptKey, input.PasswordKey);
                var existsPasswordKey  = await _cache.ExistsAsync(passwordEncryptKey);

                if (existsPasswordKey)
                {
                    var secretKey = await _cache.GetAsync(passwordEncryptKey);

                    if (secretKey.IsNull())
                    {
                        return(ResponseOutput.NotOk("解密失败!", 1));
                    }
                    input.Password = DesEncrypt.Decrypt(input.Password, secretKey);
                    await _cache.DelAsync(passwordEncryptKey);
                }
                else
                {
                    return(ResponseOutput.NotOk("解密失败!", 1));
                }
            }
            #endregion

            var password = MD5Encrypt.Encrypt32(input.Password);
            if (user.Password != password)
            {
                return(ResponseOutput.NotOk("密码输入有误!", 4));
            }

            var authLoginOutput = _mapper.Map <AuthLoginOutput>(user);

            return(ResponseOutput.Ok(authLoginOutput));
        }