Custom CAPTCHA Image Action Result for ASP.NET MVC

 

In this post you will see how to create CAPTCHA style image using ActionResult object introduced in April ASP.Net source refresh at code plex

Create CAPTCHAImageActionResult to render generated image with random text
Here is the code of custom action result class which do all the work to generate image .Nice thing about this is  back ground color, font color and random text are properties and can be set by controller  based color scheme of your web site.

 

Create Controller action to process request for CAPTCHA image.
Create ASP.Net MVC controller class to generate random text and then to return custom CAPTCHAImageActionResult object. Here is the code how it looks like. In this example I am generating simple random text with some numbers and alphabets

Use in view pages

Now it is time to use this in other view pages, add below line in any of view page. In this example I have used in homepage.


<img alt="CAPTCHA Image" src="<%=Url.Action("Index","CAPTCHAImage") %>"/>

Here is how the screen looks like.  

 You can download sample code here

Published Tuesday, May 13, 2008 10:23 PM by Karlapudi

Comments

# re: Custom CAPTCHA Image Action Result for ASP.NET MVC

Monday, May 19, 2008 10:44 AM by Mike

Really looking forward to getting my hands on this stuff.

# re: Custom CAPTCHA Image Action Result for ASP.NET MVC

Wednesday, November 26, 2008 2:11 AM by yogesh_kolpe

Hi.....Very nice code.

I already used ur code in my project its working fine.

But am trying to refresh captcha image on click, if image is not properly visible.

<div>

       Captcha:

       <div id="captchaimage">

          <img alt="CAPTCHAImage" src="<%=Url.Action("Captcha","Account")%>" />            

       </div>        

       <%= Ajax.ActionLink("Try a different image", "Captcha", "Account", new AjaxOptions { UpdateTargetId = "captchaimage" })%>

</div>

when the first time page loads it renders captcha very well but after clicking on link its not rendering captcha image.

I am using MVC beta.

Any help or suggestion.....

Thanks In Advance.

# re: Custom CAPTCHA Image Action Result for ASP.NET MVC

Friday, April 10, 2009 3:26 PM by nick_nogetb

www.message_cogetdron.com

# re: Custom CAPTCHA Image Action Result for ASP.NET MVC

Tuesday, April 21, 2009 1:21 PM by zure

Nice job!

A nice feature could be adding a remember me checkbox, so that we don't need to enter the captcha every time.

# re: Custom CAPTCHA Image Action Result for ASP.NET MVC

Monday, April 27, 2009 9:01 AM by imran

zure, a remember me option  would defeat the purpose of having a captcha.

# re: Custom CAPTCHA Image Action Result for ASP.NET MVC

Monday, May 25, 2009 12:16 PM by Aziz

hi there!

Ur captcha is greate. but i have some problem u know, if i refresh it, and Sessiom["RandomNumber"]'s value will be twice! i mean, i checked it by DEbug in VS 2008. Break point came to action result two time. So Session and input text never will be suitable

Leave a Comment

(required) 
(required) 
(optional)
(required)