Recaptchable
@Html.Partial("Recaptcha") @Html.ValidationMessageFor(m => m.Recaptcha)
web.config
/configuration/appSettings
<!-- reCaptcha--> <add key="Recaptcha.PublicKey" value="your-public-key" /> <add key="Recaptcha.PrivateKey" value="your-private-key" /> <add key="Recaptcha.ValidationUrl" value="http://www.google.com/recaptcha/api/verify" />
/Views/Shared/
Recaptcha.cshtml
@using System.Configuration @{ var key = ConfigurationManager.AppSettings["Recaptcha.PublicKey"]; } <script type="text/javascript" src="https://www.google.com/recaptcha/api/challenge?k=@(key)"> </script> <noscript> <iframe src="https://www.google.com/recaptcha/api/noscript?k=@(key)" height="300" width="500" frameborder="0"></iframe><br /> <textarea name="recaptcha_challenge_field" rows="3" cols="40"> </textarea> <input type="hidden" name="recaptcha_response_field" value="manual_challenge" /> </noscript>
WebRequestHelper.cs
using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Web; public class WebRequestHelper { public enum RequestMethod { Post, Get } readonly WebRequest _request; readonly List<string> _queryData; public WebRequestHelper(string url) { _request = WebRequest.Create(url); //_request.Method = "POST"; Method = RequestMethod.Post; _queryData = new List<string>(); } /// <summary> /// Defaults to POST /// </summary> public RequestMethod Method { get { return (RequestMethod)Enum.Parse(typeof (RequestMethod), _request.Method); } set { _request.Method = value.ToString(); } } public void Add(string key, string value) { _queryData.Add(String.Format("{0}={1}", key, HttpUtility.UrlEncode(value))); } public string GetResponse() { // Set the encoding type _request.ContentType = "application/x-www-form-urlencoded"; // Build a string containing all the parameters //var parameters = String.Join("&", (String[])_queryData.ToArray(typeof(string))); var parameters = string.Join("&", _queryData); _request.ContentLength = parameters.Length; // We write the parameters into the request var sw = new StreamWriter(_request.GetRequestStream()); sw.Write(parameters); sw.Close(); // Execute the query var response = _request.GetResponse(); var sr = new StreamReader(response.GetResponseStream()); return sr.ReadToEnd(); } }
Recaptchable.cs
using System.ComponentModel.DataAnnotations; using System.Configuration; using System.Web; public abstract class Recaptchable { [CustomValidation(typeof(RecaptchaValidator), "Validate")] public Recaptchable Recaptcha { get { return this; } } public string recaptcha_challenge_field { get; set; } public string recaptcha_response_field { get; set; } public static class RecaptchaValidator { public static ValidationResult Validate(Recaptchable recaptcha) { // In the event the RECAPTCHA_CHALLENGE_FIELD is null, we assume it is because // the recaptcha control is not displayed on the page; we therefore return // a successful validation. if (recaptcha.recaptcha_challenge_field == null) return ValidationResult.Success; var url = ConfigurationManager.AppSettings["Recaptcha.ValidationUrl"]; var privateKey = ConfigurationManager.AppSettings["Recaptcha.PrivateKey"]; var remoteIp = HttpContext.Current.Request.UserHostAddress; var request = new WebRequestHelper(url); request.Add("privatekey", privateKey); request.Add("remoteip", remoteIp); request.Add("challenge", recaptcha.recaptcha_challenge_field); request.Add("response", recaptcha.recaptcha_response_field); var items = (request.GetResponse() + "\n").Split('\n'); return items[0] == "true" ? ValidationResult.Success : new ValidationResult("You did not type the verification word correctly."); } } }
ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.