public async Task <bool> SendVerificationCodeAsync(string phoneNumber, VerificationCodeType type, string data = null)
        {
            string code = GenerateCode();

            bool isTestMode = phoneNumber.StartsWith("+4475555");

            var verificationToken = new VerificationToken
            {
                CreateDate  = DateTime.UtcNow,
                TokenHash   = _hashService.GetHash(isTestMode ? "1111" : code),
                PhoneNumber = phoneNumber,
                Type        = type,
                Data        = data
            };

            _unitOfWork.Repository <VerificationToken>().Insert(verificationToken);
            _unitOfWork.SaveChanges();

            if (!isTestMode)
            {
                await SendMessageAsync(phoneNumber, $"Your verification code: {code}");
            }

            _logger.LogError($"Your verification code: {code}");
            return(true);
        }
Пример #2
0
 /// <summary>
 /// 获取验证码缓存key
 /// </summary>
 /// <param name="accountName">用户登录名</param>
 /// <param name="type">类型</param>
 /// <param name="systemCode">系统code</param>
 /// <returns></returns>
 public static string GetVerificationCodeRedisKey(string accountName, VerificationCodeType type, string systemCode)
 {
     return($"{systemCode}:VerificationCode:{type.ToString()}:{accountName}");
 }
Пример #3
0
 public VerificationCode(string accountName, VerificationCodeType type)
 {
     Type        = type;
     AccountName = accountName;
     Code        = GetRandCode(6);
 }
        public async Task <bool> SendVerificationCodeAsync(ApplicationUser user, string phoneNumber, VerificationCodeType type, string data = null)
        {
            string code = GenerateCode();

            bool isTestMode = phoneNumber.StartsWith("+4475555");

            user.VerificationTokens.ToList().ForEach(x => _unitOfWork.Repository <VerificationToken>().Delete(x));

            user.VerificationTokens.Add(new VerificationToken
            {
                CreateDate  = DateTime.UtcNow,
                TokenHash   = _hashService.GetHash(isTestMode ? "1111" : code),
                PhoneNumber = phoneNumber,
                Type        = type,
                Data        = data
            });

            _unitOfWork.Repository <ApplicationUser>().Update(user);
            _unitOfWork.SaveChanges();

            if (!isTestMode)
            {
                await SendMessageAsync(phoneNumber, $"Your verification code: {code}");
            }

            _logger.LogError($"Your verification code: {code}");
            return(true);
        }
Пример #5
0
        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <param name="code"></param>
        /// <param name="type"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public MemoryStream Create(out string code, VerificationCodeType type = VerificationCodeType.Number, int length = 4)
        {
            switch (type)
            {
            case VerificationCodeType.Number:
                code = GetRandomForNumber(length);
                break;

            case VerificationCodeType.Letter:
                code = GetRandomForLetter(length);
                break;

            case VerificationCodeType.NumberAndLetter:
                code = GetRandomForNumberAndLetter(length);
                break;

            case VerificationCodeType.Hanzi:
                code = GetRandomForHanzi(length);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }

            var random = new Random();

            // 验证码颜色集合
            Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };

            // 验证码字体集合
            string[] fonts = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };

            // 定义图像的大小,生成图像的实例
            var img = new Bitmap(code.Length * 18, 32);

            var g = Graphics.FromImage(img);

            g.Clear(Color.White); // 背景设为白色

            // 在随机位置画背景点
            for (var i = 0; i < 100; i++)
            {
                var x = random.Next(img.Width);
                var y = random.Next(img.Height);
                g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
            }
            // 验证码绘制在g中
            for (var i = 0; i < code.Length; i++)
            {
                var   cindex = random.Next(7);                              // 随机颜色索引值
                var   findex = random.Next(5);                              // 随机字体索引值
                var   f      = new Font(fonts[findex], 15, FontStyle.Bold); // 字体
                Brush b      = new SolidBrush(c[cindex]);                   // 颜色
                var   ii     = 4;
                if ((i + 1) % 2 == 0)                                       // 控制验证码不在同一高度
                {
                    ii = 2;
                }
                g.DrawString(code.Substring(i, 1), f, b, 3 + (i * 12), ii); // 绘制一个验证字符
            }
            var ms = new MemoryStream();

            img.Save(ms, ImageFormat.Png);// 将此图像以Png图像文件的格式保存到流中

            //回收资源
            g.Dispose();
            img.Dispose();

            return(ms);
        }