public CaptchaResultModel GetCaptcha()
        {
            var randomValue = CaptchaHelper.GetValidateCode(5);
            var imageData   = CaptchaHelper.CreateBase64Image(randomValue);
            var key         = Guid.NewGuid().ToString();

            _cachingProvider.Set(key, randomValue, TimeSpan.FromMinutes(2));
            return(new CaptchaResultModel(key, imageData));
        }
        public async Task <CaptchaResultModel> GetCaptcha()
        {
            var randomValue = CaptchaHelper.GetValidateCode(5);
            var imageData   = CaptchaHelper.CreateBase64Image(randomValue);
            var key         = Guid.NewGuid().ToString();
            await _cachingProvider.SetAsync(key, randomValue, TimeSpan.FromMinutes(2));

            return(new CaptchaResultModel
            {
                CaptchaKey = key,
                CaptchaData = imageData
            });
        }
Beispiel #3
0
        public IActionResult GetCaptcha()
        {
            // 验证码的值
            var code = _captchaHelper.GetValidateCode();

            // 生成验证码图片流
            var stream = _captchaHelper.CreateImageStream(code);

            // 保存到缓存
            // todo 整理缓存服务,配置化过期时间
            var codeId = Guid.NewGuid().ToString();

            _memoryCache.Set(codeId, code, new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(2)));

            // 将id保存到header返回客户端
            Response.Headers.Add(CommonConstants.CaptchaCodeId, codeId.ToString());
            return(File(stream.ToArray(), @"image/png"));
        }