Mondor’s ASP.Net CAPTCHA control with URL Rewriting

Mondor’s ASP.Net CAPTCHA Control is, by far, one of the great free CAPTCHA controls available for .Net community.

UrlRewritingNet.UrlRewrite is a super cool URL rewriting plugin that I’ve come to using on almost each and every project of mine.

Now the problem was created when I tried to use both of them on the same project. I configured the <httpHandler/> as specified on Mondor’s page, but the CAPTCHA image never showed up.

Why? Because, UrlRewritingNet module was intercepting all requests to non existent items, including CaptchaImage.axd which is used by Mondor’s CAPTCHA control, before the httpHandler can get hands on the request pipeline.

While I tried a couple methods to overcome the situation including this one typical method where I analyzed the code using Reflector and then writing a custom GenericHandler(CaptchaImage.ashx) and created a stream filter to replace CaptchaImage.axd with CaptchaImage.ashx.

Finally, the solution came from where the problem was created. The UrlRewritingNet module. Here are the steps that you can use to resolve the problem.

First of all, create a Generic Handler, say, CaptchaImage.ashx at the root of your application with the following code.

<%@ WebHandler Language="VB" Class="CaptchaImageHelperHandler" %>

Imports MSCaptcha

Public Class CaptchaImageHelperHandler
    Inherits MSCaptcha.CaptchaImageHandler
    

End Class

Now, all we need to do is rewrite CaptchaImage.axd to this file using, you guessed it right, UrlRewritingNet module.

Here is a sample configuration line for you.

<add name="CAPTCHA" 
	virtualUrl="^~/(.+)CaptchaImage.axd"
	destinationUrl="/CaptchaImage.ashx"
	ignoreCase="true"
	rewriteUrlParameter="ExcludeFromClientQueryString" />

And you are done. You don’t have to modify your existing code and for a person like me, that is a greatttt relief.

Website Security Test