Ejemplo n.º 1
0
        public void ProcessRequest(HttpContext context)
        {
            HttpContextBase currentContext = new HttpContextWrapper(context);
            bool            isremove       = false;

            if (!string.IsNullOrEmpty(context.Request.QueryString["isremove"]))
            {
                bool.TryParse(context.Request.QueryString["isremove"], out isremove);
            }

            string cookieName                = CaptchaSettings.Instance().CaptchaCookieName;
            bool   enableLineNoise           = CaptchaSettings.Instance().EnableLineNoise;
            CaptchaCharacterSet characterSet = CaptchaSettings.Instance().CharacterSet;
            int    minCharacterCount         = CaptchaSettings.Instance().MinCharacterCount;
            int    maxCharacterCount         = CaptchaSettings.Instance().MaxCharacterCount;
            string generatedKey              = string.Empty;
            bool   addCooikes                = false;
            //创建或从缓存取验证码
            string key = null;

            if (context.Request.Cookies[cookieName] != null)
            {
                key = context.Request.Cookies[cookieName].Value;
            }
            if (isremove && !string.IsNullOrEmpty(key))
            {
                VerificationCodeManager.GetCachedTextAndForceExpire(currentContext, getCurrentLevelKey(key));
            }

            System.IO.MemoryStream ms = null;
            if (!string.IsNullOrEmpty(key))
            {
                ms = VerificationCodeManager.GetCachedImageStream(getCurrentLevelKey(key));
            }

            if (ms == null)
            {
                Size size = new Size(85, 30);
                VerificationCodeImage image = VerificationCodeManager.GenerateAndCacheImage(currentContext, size, 300, out generatedKey, characterSet, enableLineNoise, minCharacterCount, maxCharacterCount);

                ms = VerificationCodeManager.GetCachedImageStream(getCurrentLevelKey(generatedKey));
                VerificationCodeManager.CacheText(currentContext, image.Text, getCurrentLevelKey(generatedKey), false, 300);
                addCooikes = true;
            }
            if (addCooikes)
            {
                HttpCookie cookie = new HttpCookie(cookieName, generatedKey);
                context.Response.Cookies.Add(cookie);
            }
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.ContentType = "image/Jpeg";
            context.Response.BinaryWrite(ms.ToArray());
            //context.Response.Flush();
            context.Response.End();
        }
Ejemplo n.º 2
0
        private static VerificationCodeImage GenerateRandomAutoInputProtectionImage(Size size, CaptchaCharacterSet characterSet, int?maximumCharacters, int?minimumCharacters)
        {
            if (size.Width < 1 || size.Height < 1)
            {
                throw new ArgumentOutOfRangeException("size", size, "Errors.SizePositiveIntRequired");
            }

            EnsureProviders();
            VerificationCodeImage image = null;

            lock (VerificationCodeImageLockObj)
            {
                image = imageProvider.GenerateRandomAutoInputProtectionImage(size, textProvider, characterSet, maximumCharacters, minimumCharacters);
            }
            image.filters = filterProviders;

            return(image);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///创建验证码以及图片的数据流
        /// </summary>
        private static bool CreateImageStream(ref int attempt, HttpContextBase context, VerificationCodeImage aipImage, bool isUseLineNoise, out MemoryStream stream)
        {
            stream = null;

            using (Image image = aipImage.CreateCompositeImage(isUseLineNoise))
            {
                try
                {
                    stream = new MemoryStream(image.Width * image.Height);

                    image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                catch (ArgumentException)
                {
                    if (stream != null)
                    {
                        stream.Dispose();
                    }
                    attempt++;
                    if (attempt == 3)
                    {
                        throw;
                    }
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///创建验证码以及图片的数据流
        /// </summary>
        private static bool CreateImageStream(ref int attempt, HttpContextBase context, VerificationCodeImage aipImage, bool isUseLineNoise, out MemoryStream stream)
        {
            stream = null;

            using (Image image = aipImage.CreateCompositeImage(isUseLineNoise))
            {
                try
                {
                    stream = new MemoryStream(image.Width * image.Height);

                    image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                catch (ArgumentException)
                {
                    if (stream != null)
                        stream.Dispose();
                    attempt++;
                    if (attempt == 3)
                        throw;
                    return false;
                }
            }

            return true;
        }