patterncsharpMinor
Google reCAPTCHA Validator
Viewed 0 times
recaptchavalidatorgoogle
Problem
This entire class came out of a chat discussion, and I'm curious on how it looks. (This is like literally 30 minutes of development time.)
The idea is to allow very easy, quick implementations of Google's reCAPTCHA ("I am not a robot") checkbox CAPTCHA algorithm. It only requires minor work from the implementor to make it work properly, and that was the point.
Warning: Minimal implementation effort required.
```
public class ReCaptchaValidator
{
private const string _HeadScriptInclude = "";
private const string _ReCaptchaLocationInclude = "";
private readonly string _ReCaptchaSecret;
private readonly string _ReCaptchaSiteKey;
///
/// Returns the script to be included in the <head> of the page.
///
public string HeadScriptInclude { get { return _HeadScriptInclude; } }
///
/// Use this to get or set any extra classes that should be added to the <div> that is created by the .
///
public List ExtraClasses { get; set; }
///
/// Returns the <div> that should be inserted in the HTML where the reCAPTCHA should go.
///
///
/// I'm still not sure if this should be a method or not.
///
public string ReCaptchaLocationInclude { get { return _ReCaptchaLocationInclude.Replace("%SITEKEY%", _ReCaptchaSiteKey).Replace("%EXTRACLASSES%", string.Join(" ", ExtraClasses)); } }
///
/// Creates a new instance of the .
///
/// The reCAPTCHA secret.
/// The reCAPTCHA site key.
public ReCaptchaValidator(string reCaptchaSecret, string reCaptchaSiteKey)
{
_ReCaptchaSecret = reCaptchaSecret;
_ReCaptchaSiteKey = reCaptchaSiteKey;
}
///
/// Determines if the reCAPTCHA response in a NameValueCollection passed validation.
///
/// The Request.Form to validate.
/// A boolean value indicating success.
public bool Validate(NameValueCollection form)
{
string reCaptchaSecret = _ReCaptchaSecret;
string reCaptchaResponse =
The idea is to allow very easy, quick implementations of Google's reCAPTCHA ("I am not a robot") checkbox CAPTCHA algorithm. It only requires minor work from the implementor to make it work properly, and that was the point.
Warning: Minimal implementation effort required.
```
public class ReCaptchaValidator
{
private const string _HeadScriptInclude = "";
private const string _ReCaptchaLocationInclude = "";
private readonly string _ReCaptchaSecret;
private readonly string _ReCaptchaSiteKey;
///
/// Returns the script to be included in the <head> of the page.
///
public string HeadScriptInclude { get { return _HeadScriptInclude; } }
///
/// Use this to get or set any extra classes that should be added to the <div> that is created by the .
///
public List ExtraClasses { get; set; }
///
/// Returns the <div> that should be inserted in the HTML where the reCAPTCHA should go.
///
///
/// I'm still not sure if this should be a method or not.
///
public string ReCaptchaLocationInclude { get { return _ReCaptchaLocationInclude.Replace("%SITEKEY%", _ReCaptchaSiteKey).Replace("%EXTRACLASSES%", string.Join(" ", ExtraClasses)); } }
///
/// Creates a new instance of the .
///
/// The reCAPTCHA secret.
/// The reCAPTCHA site key.
public ReCaptchaValidator(string reCaptchaSecret, string reCaptchaSiteKey)
{
_ReCaptchaSecret = reCaptchaSecret;
_ReCaptchaSiteKey = reCaptchaSiteKey;
}
///
/// Determines if the reCAPTCHA response in a NameValueCollection passed validation.
///
/// The Request.Form to validate.
/// A boolean value indicating success.
public bool Validate(NameValueCollection form)
{
string reCaptchaSecret = _ReCaptchaSecret;
string reCaptchaResponse =
Solution
Some quick shots at the code
-
by retrieving the
-
with
and for the IDE's love add a
-
by retrieving the
ReCaptchaLocationInclude property an ArgumentNullException is thrown, because you didn't initialize the ExtraClasses property. I would also like to suggest changing this property from autoimplemented to a normal one, so you can validate any set value. -
bool passedReCaptcha = false; is not really a good name here. so I wouldn't use this variable at all. Instead I would replace this if (reCaptchaResult.IndexOf("\"success\": true") > 0)
passedReCaptcha = true;with
return (reCaptchaResult.IndexOf("\"success\": true") > 0);and for the IDE's love add a
return false; at the end of the method. If you don't want to do this, it is fine too, but you should replace the former if with passedReCaptcha = (reCaptchaResult.IndexOf("\"success\": true") > 0);Code Snippets
if (reCaptchaResult.IndexOf("\"success\": true") > 0)
passedReCaptcha = true;return (reCaptchaResult.IndexOf("\"success\": true") > 0);Context
StackExchange Code Review Q#100594, answer score: 2
Revisions (0)
No revisions yet.