Example #1
0
        /// <summary>
        /// Renders the Captcha Image.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext"></see> object that provides
        /// references to the intrinsic server objects (for example, Request, Response, Session, and Server)
        /// used to service HTTP requests.</param>
        public void ProcessRequest(HttpContext context)
        {
            HttpApplication application          = context.ApplicationInstance;
            string          encryptedCaptchaInfo = application.Request.QueryString["spec"];
            CaptchaInfo     captcha = CaptchaInfo.FromEncryptedString(encryptedCaptchaInfo);

            string textToRender = captcha.Text;

            if (string.IsNullOrEmpty(textToRender))
            {
                application.Response.StatusCode = 404;
                application.Response.End();
            }
            else
            {
                using (var captchaImage = new CaptchaImage())
                {
                    captchaImage.Width    = captcha.Width;
                    captchaImage.Height   = captcha.Height;
                    captchaImage.FontWarp = captcha.WarpFactor;
                    captchaImage.Font     = captcha.FontFamily;
                    captchaImage.Text     = textToRender;
                    captchaImage.Image.Save(application.Context.Response.OutputStream, ImageFormat.Jpeg);
                }
                application.Response.ContentType = "image/jpeg";
                application.Response.StatusCode  = 200;
                application.Response.End();
            }
        }
Example #2
0
        public void CanRoundTripCaptchaInfo()
        {
            DateTime date = DateTime.ParseExact(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture), "yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture);

            CaptchaInfo info = new CaptchaInfo("My Test");
            info.WarpFactor = CaptchaImage.FontWarpFactor.High;
            info.DateGenerated = date;

            string encrypted = info.ToEncryptedString();
            Assert.IsTrue(encrypted.IndexOf("My Test") < 0);
            info = CaptchaInfo.FromEncryptedString(encrypted);
            Assert.AreEqual("My Test", info.Text);
            Assert.AreEqual(CaptchaImage.FontWarpFactor.High, info.WarpFactor);
            Assert.AreEqual(date, info.DateGenerated);
        }
Example #3
0
        /// <summary>
        /// Reconstructs an instance of this type from an encrypted serialized string.
        /// </summary>
        /// <param name="encrypted"></param>
        public static CaptchaInfo FromEncryptedString(string encrypted)
        {
            string decrypted = CaptchaBase.DecryptString(encrypted);

            string[] values = decrypted.Split('|');

            var info = new CaptchaInfo
            {
                Width         = int.Parse(values[0], CultureInfo.InvariantCulture),
                Height        = int.Parse(values[1], CultureInfo.InvariantCulture),
                WarpFactor    = (CaptchaImage.FontWarpFactor)Enum.Parse(typeof(CaptchaImage.FontWarpFactor), values[2]),
                FontFamily    = values[3],
                Text          = values[4],
                DateGenerated = DateTime.ParseExact(values[5], "yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture)
            };

            return(info);
        }
Example #4
0
        /// <summary>
        /// Reconstructs an instance of this type from an encrypted serialized string.
        /// </summary>
        /// <param name="encrypted"></param>
        public static CaptchaInfo FromEncryptedString(string encrypted)
        {
            string decrypted = CaptchaBase.DecryptString(encrypted);
            string[] values = decrypted.Split('|');

            var info = new CaptchaInfo
            {
                Width = int.Parse(values[0], CultureInfo.InvariantCulture),
                Height = int.Parse(values[1], CultureInfo.InvariantCulture),
                WarpFactor = (CaptchaImage.FontWarpFactor)Enum.Parse(typeof(CaptchaImage.FontWarpFactor), values[2]),
                FontFamily = values[3],
                Text = values[4],
                DateGenerated = DateTime.ParseExact(values[5], "yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture)
            };
            return info;
        }