void System.Web.IHttpHandler.ProcessRequest(System.Web.HttpContext context) { HttpApplication app = context.ApplicationInstance; //-- get the unique GUID of the captcha; this must be passed in via the querystring string guid = app.Request.QueryString["guid"]; CaptchaImage ci = null; if (guid != "") { if (string.IsNullOrEmpty(app.Request.QueryString["s"])) { ci = (CaptchaImage)HttpRuntime.Cache.Get(guid); } else { ci = (CaptchaImage)HttpContext.Current.Session[guid]; } } if (ci == null) { app.Response.StatusCode = 404; context.ApplicationInstance.CompleteRequest(); return; } //-- write the image to the HTTP output stream as an array of bytes using (Bitmap b = ci.RenderImage()) { b.Save(app.Context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); } app.Response.ContentType = "image/jpeg"; app.Response.StatusCode = 200; context.ApplicationInstance.CompleteRequest(); }
private CaptchaImage GetCachedCaptcha(string guid) { CaptchaImage functionReturnValue = null; if (_cacheStrategy == CacheType.HttpRuntime) { return((CaptchaImage)HttpRuntime.Cache.Get(guid)); } else { return((CaptchaImage)HttpContext.Current.Session[guid]); } return(functionReturnValue); }
/// <summary> /// Validate the user's text against the CAPTCHA text /// </summary> public void ValidateCaptcha() { string userEntry = tbUserEntry.Text; if (!Visible | !Enabled) { _userValidated = true; return; } //-- retrieve the previous captcha from the cache to inspect its properties CaptchaImage ci = GetCachedCaptcha(_prevguid); if (ci == null) { SetErrorMessage("The code you typed has expired after " + this.CaptchaMaxTimeout + " seconds."); _userValidated = false; return; } //-- was it entered too quickly? if (this.CaptchaMinTimeout > 0) { if ((ci.RenderedAt.AddSeconds(this.CaptchaMinTimeout) > System.DateTime.Now)) { _userValidated = false; SetErrorMessage("Code was typed too quickly. Wait at least " + this.CaptchaMinTimeout + " seconds."); RemoveCachedCaptcha(_prevguid); return; } } if (string.Compare(userEntry, ci.Text, true) != 0) { //((System.Web.UI.IValidator) this).ErrorMessage = "The code you typed does not match the code in the image."; SetErrorMessage("The code you typed does not match the code in the image."); _userValidated = false; RemoveCachedCaptcha(_prevguid); return; } _userValidated = true; RemoveCachedCaptcha(_prevguid); }