/// <summary> /// Creates an instance of the <see cref="RecaptchaHtmlHelper"/> class. /// </summary> /// <param name="publicKey">Sets the public key to be part of the recaptcha HTML.</param> public RecaptchaHtmlHelper(string publicKey) { if (String.IsNullOrEmpty(publicKey)) { throw new InvalidOperationException("Public key cannot be null or empty."); } this.PublicKey = RecaptchaKeyHelper.ParseKey(publicKey); UseSsl = HttpContext.Current.Request.Url.AbsoluteUri.StartsWith("https"); }
/// <summary> /// Creates an instance of the <see cref="RecaptchaHtmlHelper"/> class. /// </summary> /// <param name="publicKey">Sets the public key of the recaptcha HTML.</param> /// <param name="theme">Sets the theme of the recaptcha HTML.</param> /// <param name="language">Sets the language of the recaptcha HTML.</param> /// <param name="tabIndex">Sets the tab index of the recaptcha HTML.</param> public RecaptchaHtmlHelper(string publicKey, RecaptchaTheme theme, string language, int tabIndex) { this.PublicKey = RecaptchaKeyHelper.ParseKey(publicKey); if (String.IsNullOrEmpty(this.PublicKey)) { throw new InvalidOperationException("Public key cannot be null or empty."); } this.Theme = theme; this.Language = language; this.TabIndex = tabIndex; UseSsl = HttpContext.Current.Request.Url.AbsoluteUri.StartsWith("https"); }
/// <summary> /// Verifies whether the user's response to the recaptcha request is correct. /// </summary> /// <returns>Returns the result as a value of the <see cref="RecaptchaVerificationResult"/> enum.</returns> public Task <RecaptchaVerificationResult> VerifyRecaptchaResponseTaskAsync() { if (string.IsNullOrEmpty(_Challenge)) { return(FromTaskResult <RecaptchaVerificationResult>(RecaptchaVerificationResult.ChallengeNotProvided)); } if (string.IsNullOrEmpty(Response)) { return(FromTaskResult <RecaptchaVerificationResult>(RecaptchaVerificationResult.NullOrEmptyCaptchaSolution)); } Task <RecaptchaVerificationResult> result = Task <RecaptchaVerificationResult> .Factory.StartNew(() => { string privateKey = RecaptchaKeyHelper.ParseKey(PrivateKey); string postData = String.Format("privatekey={0}&remoteip={1}&challenge={2}&response={3}", privateKey, this.UserHostAddress, this._Challenge, this.Response); byte[] postDataBuffer = System.Text.Encoding.ASCII.GetBytes(postData); Uri verifyUri = null; if (!UseSsl) { verifyUri = new Uri("http://api-verify.recaptcha.net/verify", UriKind.Absolute); } else { verifyUri = new Uri("https://api-verify.recaptcha.net/verify", UriKind.Absolute); } try { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(verifyUri); webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.ContentLength = postDataBuffer.Length; webRequest.Method = "POST"; IWebProxy proxy = WebRequest.GetSystemWebProxy(); proxy.Credentials = CredentialCache.DefaultCredentials; webRequest.Proxy = proxy; Stream requestStream = webRequest.GetRequestStream(); requestStream.Write(postDataBuffer, 0, postDataBuffer.Length); HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); string[] responseTokens = null; using (StreamReader sr = new StreamReader(webResponse.GetResponseStream())) { responseTokens = sr.ReadToEnd().Split('\n'); } if (responseTokens.Length == 2) { Boolean success = responseTokens[0].Equals("true", StringComparison.CurrentCulture); if (success) { return(RecaptchaVerificationResult.Success); } else { if (responseTokens[1].Equals("incorrect-captcha-sol", StringComparison.CurrentCulture)) { return(RecaptchaVerificationResult.IncorrectCaptchaSolution); } else if (responseTokens[1].Equals("invalid-site-private-key", StringComparison.CurrentCulture)) { return(RecaptchaVerificationResult.InvalidPrivateKey); } else if (responseTokens[1].Equals("invalid-request-cookie", StringComparison.CurrentCulture)) { return(RecaptchaVerificationResult.InvalidCookieParameters); } } } return(RecaptchaVerificationResult.UnknownError); } catch (Exception ex) { throw ex; } }); return(result); }
/// <summary> /// Verifies whether the user's response to the recaptcha request is correct. /// </summary> /// <returns>Returns the result as a value of the <see cref="RecaptchaVerificationResult"/> enum.</returns> public RecaptchaVerificationResult VerifyRecaptchaResponse() { if (string.IsNullOrEmpty(_Challenge)) { return(RecaptchaVerificationResult.ChallengeNotProvided); } if (string.IsNullOrEmpty(Response)) { return(RecaptchaVerificationResult.NullOrEmptyCaptchaSolution); } string privateKey = RecaptchaKeyHelper.ParseKey(PrivateKey); string postData = String.Format("privatekey={0}&remoteip={1}&challenge={2}&response={3}", privateKey, this.UserHostAddress, this._Challenge, this.Response); byte[] postDataBuffer = System.Text.Encoding.ASCII.GetBytes(postData); Uri verifyUri = null; // COQ El Colombiano: Jan.14/2014 // URL to verify is changed to https://www.google.com/recaptcha/api and of course parameters are remapped. string URI = "//www.google.com/recaptcha/api/verify"; if (!UseSsl) { verifyUri = new Uri("http:" + URI, UriKind.Absolute); } else { //verifyUri = new Uri("https:" + URI, UriKind.Absolute); verifyUri = new Uri("https:" + URI, UriKind.Absolute); } try { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(verifyUri); webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.ContentLength = postDataBuffer.Length; webRequest.Method = "POST"; IWebProxy proxy = WebRequest.GetSystemWebProxy(); proxy.Credentials = CredentialCache.DefaultCredentials; webRequest.Proxy = proxy; Stream requestStream = webRequest.GetRequestStream(); requestStream.Write(postDataBuffer, 0, postDataBuffer.Length); HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); string[] responseTokens = null; using (StreamReader sr = new StreamReader(webResponse.GetResponseStream())) { responseTokens = sr.ReadToEnd().Split('\n'); } if (responseTokens.Length == 2) { Boolean success = responseTokens[0].Equals("true", StringComparison.CurrentCulture); if (success) { return(RecaptchaVerificationResult.Success); } else { if (responseTokens[1].Equals("incorrect-captcha-sol", StringComparison.CurrentCulture)) { return(RecaptchaVerificationResult.IncorrectCaptchaSolution); } else if (responseTokens[1].Equals("invalid-site-private-key", StringComparison.CurrentCulture)) { return(RecaptchaVerificationResult.InvalidPrivateKey); } else if (responseTokens[1].Equals("invalid-request-cookie", StringComparison.CurrentCulture)) { return(RecaptchaVerificationResult.InvalidCookieParameters); } } } return(RecaptchaVerificationResult.UnknownError); } catch (Exception ex) { throw ex; } }