Exemplo n.º 1
0
        public async Task <FileContentResult> GenerateCaptcha(string name, CaptchaType?t, int?tid)
        {
            if (!t.HasValue ||
                t.Value == CaptchaType.Defulat ||
                name.IsNullOrWhiteSpace())
            {
                return(null);
            }

            var captchaConfig = await SettingManager.GetCaptchaConfig(t.Value, tid);

            if (!captchaConfig.Enabled)
            {
                return(null);
            }

            var imgStream = _vierificationCodeService.Create(out string code, (ValidateCodeType)captchaConfig.Type, captchaConfig.Length);

            // 分租户获取缓存
            var cacheKey = CaptchaHelper.CreateCacheKey(t.Value, tid);
            var cache    = _cacheManager.GetCache(cacheKey);

            // 存值,过期时间3分钟
            await cache.SetAsync(name, code.ToLower(), null, TimeSpan.FromMinutes(3));

            //
            Response.Body.Dispose();
            return(File(imgStream.ToArray(), MimeTypeNames.ImagePng));
        }
Exemplo n.º 2
0
        /// <summary>
        ///     检查图形验证码功能
        /// </summary>
        /// <param name="type">图形验证码的类型</param>
        /// <param name="code">待验证的code</param>

        protected async Task <bool> VerifyImgCodeAsync(CaptchaType type, string code)
        {
            if (string.IsNullOrWhiteSpace(code))
            {
                throw new UserFriendlyException("验证码不能为空!");
            }

            //租户Id
            var tenantId = AbpSession.TenantId;
            // 分租户获取缓存
            var cacheKey = CaptchaHelper.CreateCacheKey(type, tenantId);
            //图形验证码缓存管理器
            var captchaCacheManager = CacheManager.GetCache(cacheKey);

            var sessionId = HttpContext.Request.Cookies[cacheKey];

            //获取缓存中的值
            var cacheCode = await captchaCacheManager.GetOrDefaultAsync(sessionId);



            if (cacheCode != null && code == cacheCode.ToString())
            {
                await captchaCacheManager.RemoveAsync(sessionId);

                return(true);
            }

            await captchaCacheManager.RemoveAsync(sessionId);

            throw new UserFriendlyException("验证码错误!");
        }