Exemple #1
0
        /// <summary>
        /// Generates the captcha image.
        /// </summary>
        /// <param name="helper">The helper.</param>
        /// <param name="height">The height.</param>
        /// <param name="width">The width.</param>
        /// <returns>
        /// Returns the <see cref="Uri"/> for the generated <see cref="CaptchaImage"/>.
        /// </returns>
        public static MvcHtmlString CaptchaImage(this HtmlHelper htmlHelper, string imgUrl, int height, int width)
        {
            Helpers.CaptchaImage image = new Helpers.CaptchaImage
            {
                Height = height,
                Width  = width,
            };

            HttpRuntime.Cache.Add(
                image.UniqueId,
                image,
                null,
                DateTime.Now.AddSeconds(Helpers.CaptchaImage.CacheTimeOut),
                Cache.NoSlidingExpiration,
                CacheItemPriority.NotRemovable,
                null);

            StringBuilder stringBuilder = new StringBuilder(256);

            stringBuilder.Append("<input type=\"hidden\" name=\"captcha-guid\" value=\"");
            stringBuilder.Append(image.UniqueId);
            stringBuilder.Append("\" />");
            stringBuilder.AppendLine();
            stringBuilder.Append("<img src=\"");

            //!!!
            stringBuilder.Append(imgUrl + "?id=" + image.UniqueId);
            stringBuilder.Append("\" alt=\"CAPTCHA\" width=\"");
            stringBuilder.Append(width);
            stringBuilder.Append("\" height=\"");
            stringBuilder.Append(height);
            stringBuilder.Append("\" />");

            return(new MvcHtmlString(stringBuilder.ToString()));
        }
        public static CaptchaImage GetNewCaptcha(string guid)
        {
            if (string.IsNullOrEmpty(guid))
            {
                return(null);
            }
            Helpers.CaptchaImage image = GetCachedCaptcha(guid);

            image = new Helpers.CaptchaImage
            {
                UniqueId = image.UniqueId,
                Height   = image.Height,
                Width    = image.Width,
            };
            image.UniqueId = guid;

            HttpRuntime.Cache.Remove(guid);
            HttpRuntime.Cache.Add(
                image.UniqueId,
                image,
                null,
                DateTime.Now.AddSeconds(Helpers.CaptchaImage.CacheTimeOut),
                System.Web.Caching.Cache.NoSlidingExpiration,
                System.Web.Caching.CacheItemPriority.NotRemovable,
                null);
            return((CaptchaImage)HttpRuntime.Cache.Get(guid));
        }