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
- 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'
- Enum DropDownList in ASP.NET MVC
Recent comments
- so very helpful kenny. thank
9 weeks 2 days ago - Sorry, apparently the drupal
12 weeks 3 days ago - A better method
15 weeks 4 days ago - No Source Code?
21 weeks 2 days ago - security
31 weeks 1 day ago - Nice simple solution
1 year 15 weeks ago - That's quite an interesting
1 year 32 weeks ago - Small Complaint
1 year 33 weeks ago - NIceeee
1 year 39 weeks ago - Both the SQL Server connector
2 years 16 weeks ago
Comments
#1 Anonymous
Where's the code?
#2 khuang
Sorry, apparently the drupal module for uploads got disabled somehow.