Ejemplo n.º 1
0
        public Stream CreateAuthCode(string identity, int width, int height)
        {
            if (string.IsNullOrEmpty(identity))
            {
                Enforce.Throw(new FrontInputValidateErrorException("验证码identity不能为空"));
            }

            if (width < 0)
            {
                width = 100;
            }

            if (height < 0)
            {
                height = 40;
            }

            var authCode = AuthCode.CreateAuthCode(width, height, ImageFormat.Png);

            _database.StringSetAsync(identity, authCode.Code.ToLower(), TimeSpan.FromMinutes(5));
            return(authCode.Img);
        }
Ejemplo n.º 2
0
        public async Task <string> SendAuthMail(string reSendEmailToken, string identity, string code, string mail, int codeType, string agent)
        {
            if (!Enum.IsDefined(typeof(EmailAuthCodeType), codeType))
            {
                Enforce.Throw(new FrontInputValidateErrorException("验证码邮件类型错误"));
            }
            EmailAuthCodeType mailCodeType = (EmailAuthCodeType)Enum.ToObject(typeof(EmailAuthCodeType), codeType);

            //判断是否包含重发token
            if (string.IsNullOrEmpty(reSendEmailToken))
            {
                if (string.IsNullOrEmpty(identity))
                {
                    Enforce.Throw(new FrontInputValidateErrorException("验证码identity不能为空"));
                }

                //去认证验证码,验证码通过才能发邮件
                var verifyAuthCodeResult = await this.verifyAuthCode(identity, code);

                if (!verifyAuthCodeResult)
                {
                    Enforce.Throw(new LogicErrorException("验证码错误"));
                }
            }
            else
            {
                //验证重发token,重发token通过才会发邮件
                bool verifyReSendMailToken = await this.verifyReSendAuthMailToken(mail, mailCodeType, agent, reSendEmailToken);

                if (!verifyReSendMailToken)
                {
                    Enforce.Throw(new LogicErrorException("重发邮件失败"));
                }
            }

            //验证码
            var authCode = AuthCode.CreateAuthCode(100, 40, ImageFormat.Png);

            var setCodeStatus = await _database.StringSetAsync(this.generateEmailCodeKey(mail, mailCodeType), authCode.Code.ToLower(), TimeSpan.FromMinutes(10));


            //重发token
            string token          = this.generateToken(agent);
            var    setTokenStatus = await _database.StringSetAsync(this.generateReSendMailTokenKey(mail, mailCodeType), token, TimeSpan.FromMinutes(20));


            if (setCodeStatus && setTokenStatus)
            {
                //异步发送邮件
                //_mailService.SendCodeMailAsync(mail, authCode.Code);
                //_mailService.SendMailAsync(mail,
                //    _mailConfig.CodeContent.Subject.Text,
                //    string.Format(_mailConfig.CodeContent.Body.Text, authCode.Code), true);
                //Todo:后续需要优化
                _mailService.SendMailAsync(mail,
                                           _mailConfig.CodeContent.Subject.Text,
                                           _templateHelper.GenerateContent(_mailConfig.CodeContent.Body.Text, "codeContent", new { Code = authCode.Code })
                                           , true);

                return(token);
            }


            return(string.Empty);
        }