reCAPTCHA for ASP.NET MVC that uses ModelState
Here is a reCAPTCHA control for ASP.NET MVC that uses ModelState. This code includes the ActionFilter as well as the HTML Helper for rendering the reCAPTCHA control.
To use it, add the ActionFilter to the Controller Action that will be performing the verification. You can either provide the Private Key in the web.config or explicitly state it. You can also use the IgnoreOnMobile boolean property to ignore reCAPTCHA verification if a mobile browser is detected. You can also add a function delegate that returns a boolean value to the Ignore property to add custom logic to ignore reCAPTCHA under any condition you would like.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Post post)
{
if (!ModelState.IsValid)
{
// model state and recaptcha were valid, continue onwards!
}
else
{
// model state was not valid, redirect back to source page
}
}
To render the reCAPTCHA control, simply add the Recaptcha.Mvc to the page and use the HtmlHelper.
...
<%= Html.CreateRecaptcha("recaptcha", "my_private_key", "my_public_key", "clean") %>
If you wish to set the private/public keys in the web.config you can set them in the appSettings section.
<add key="RecaptchaPrivateKey" value="my_private_key"/>
<add key="RecaptchaPublicKey" value="my_public_key"/>
<appSettings>
The source code is attached below as a Solution and Project that needs to be built. Alternatively you can just add the csharp source files and drop them into your project appropriately.
| Attachment | Size |
|---|---|
| Recaptcha.Mvc_.zip | 12.38 KB |
Recent blog posts
- Quick Memory Improvement for Firefox
- Canned VirtualPC Instances for IE 6, 7, 8 on XP/Vista
- Checking assembly dependencies for .NET
- Google's Public DNS
- Server Utility Functions for Non-Web Apps
- reCAPTCHA for ASP.NET MVC that uses ModelState
- Adding a container to ValidationSummary helper in ASP.NET MVC
- Generic XML Serialization Class
- Re-throwing Exceptions in C# with InternalPreserveStackTrace
- Solving xsd generation error: 'The element .... is missing'
Recent comments
- Reply to comment | A.D.D. In IT
0 sec ago - Reply to comment | A.D.D. In IT
1 min 55 sec ago - Reply to comment | A.D.D. In IT
1 week 3 days ago - Xorauguyna
5 weeks 2 days ago - Best tutorial i've ever read.
21 weeks 5 days ago - answer this post
22 weeks 4 days ago - Thank you
1 year 7 weeks ago - so very helpful kenny. thank
1 year 24 weeks ago - Sorry, apparently the drupal
1 year 27 weeks ago - A better method
1 year 30 weeks ago
Comments
#1 Anonymous
Where's the code?
#2 khuang
Sorry, apparently the drupal module for uploads got disabled somehow.