tags:

views:

72

answers:

1

I'm follow this tutorial to use CAPTCHA in MVC application: http://coderjournal.com/2008/03/actionfilterattribute-aspnet-mvc-captcha/

I think this was made to ealry versions of ASP.NET MVC,because it doenst work for me.

Problem is that captcha image never shows,cause the custom handler (captcha.ashx) never gets the request.

In web.config:

<httpHandlers>
            <remove verb="*" path="*.asmx"/>
            <add verb="GET" validate="false" path="captcha.ashx" type="SCE.Models.Helpers.Captcha.CaptchaHandler"/>
        </httpHandlers>

The handler:

namespace SCE.Models.Helpers.Captcha
{
    public class CaptchaHandler : IHttpHandler
    {
        public bool IsReusable
        {
            get { return true; }
        }
        public void ProcessRequest(HttpContext context)
        {
            string guid = context.Request.QueryString["guid"];
            CaptchaImage ci = CaptchaImage.GetCachedCaptcha(guid);

            if (String.IsNullOrEmpty(guid) || ci == null)
            {
                context.Response.StatusCode = 404;
                context.Response.StatusDescription = "Not found";
                context.ApplicationInstance.CompleteRequest();
                return;
            }

            using (Bitmap b = ci.RenderImage())
            {
                b.Save(context.Response.OutputStream, ImageFormat.Gif);
            }
            context.Response.ContentType = "image/png";
            context.Response.StatusCode = 200;
            context.Response.StatusDescription = "OK";
            context.ApplicationInstance.CompleteRequest();
        }
    }
}

It seem newer version of MVC has changed the way they route handler,but i dont know what exactly...

Any ideias to call the handler?

UPDATE: Here is the solution ive found: In global.asax file:

routes.IgnoreRoute("{filename}.ashx/{*pathInfo}");
+1  A: 

Using MVC 2, and ReCaptcha this was a pretty elegant solution. IMHO

http://devlicio.us/blogs/derik_whittaker/archive/2008/12/02/using-recaptcha-with-asp-net-mvc.aspx

Dustin Laine
sorry i cant use recaptcha,cause ive already try,and it doenst work for me
You could possible port this same method of implementation to whatever captcha you are using. I personally really like to be able to simple decorate my action with the captcha attribute.
Dustin Laine