Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the null reference exception which occurs when DependencyResolver.Cu... #4

Merged
merged 2 commits into from
Mar 2, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions PoliteCaptcha/ReCaptchaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace PoliteCaptcha
/// </summary>
public class ReCaptchaGenerator : ICaptchaGenerator
{
readonly IConfigurationSource configSource;
readonly IConfigurationSource _configSource;

public ReCaptchaGenerator()
: this(new DefaultConfigurationSource())
Expand All @@ -21,7 +21,7 @@ public ReCaptchaGenerator()

public ReCaptchaGenerator(IConfigurationSource configSource)
{
this.configSource = configSource;
_configSource = configSource;
}

/// <summary>
Expand All @@ -31,15 +31,16 @@ public ReCaptchaGenerator(IConfigurationSource configSource)
/// <param name="fallbackMessage">An optional message to display above the CAPTCHA when it is displayed as a fallback.</param>
/// <returns>The reCAPTCHA HTML.</returns>
public IHtmlString Generate(
HtmlHelper htmlHelper,
HtmlHelper htmlHelper,
string fallbackMessage = null)
{
if (htmlHelper == null)
{
throw new ArgumentNullException("htmlHelper");
}

var configurationSource = DependencyResolver.Current.GetService<IConfigurationSource>();

var publicApiKey = configSource.GetConfigurationValue(Const.ReCaptchaPublicKeyAppSettingKey);
IConfigurationSource configurationSource = GetConfigurationSource();
var publicApiKey = configurationSource.GetConfigurationValue(Const.ReCaptchaPublicKeyAppSettingKey);
if (publicApiKey == null)
{
if (!htmlHelper.ViewContext.HttpContext.Request.IsLocal)
Expand Down Expand Up @@ -68,9 +69,28 @@ public IHtmlString Generate(
recaptchaControl.RenderControl(htmlWriter);

var captchaHtml = htmlWriter.InnerWriter.ToString();
var template = @"<div class=""PoliteCaptcha editor-field""><span class=""field-validation-error"" data-valmsg-for=""PoliteCaptcha""><span htmlfor=""PoliteCaptcha"">{0}</span></span>{1}</div>";


const string template = @"<div class=""PoliteCaptcha editor-field""><span class=""field-validation-error"" data-valmsg-for=""PoliteCaptcha""><span htmlfor=""PoliteCaptcha"">{0}</span></span>{1}</div>";
return new MvcHtmlString(string.Format(template, fallbackMessage ?? Const.DefaulFallbackMessage, captchaHtml));
}

private IConfigurationSource GetConfigurationSource()
{
if (_configSource != null)
{
return _configSource;
}

if (DependencyResolver.Current != null)
{
var resolvedConfigSource = DependencyResolver.Current.GetService<IConfigurationSource>();
if (resolvedConfigSource != null)
{
return resolvedConfigSource;
}
}

return new DefaultConfigurationSource();
}
}
}
42 changes: 31 additions & 11 deletions PoliteCaptcha/ReCaptchaValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace PoliteCaptcha
/// </summary>
public class ReCaptchaValidator : ICaptchaValidator
{
readonly RecaptchaValidator recaptchaValidator;
readonly IConfigurationSource configSource;
readonly RecaptchaValidator _recaptchaValidator;
readonly IConfigurationSource _configSource;

public ReCaptchaValidator()
: this(new DefaultConfigurationSource())
Expand All @@ -20,8 +20,8 @@ public ReCaptchaValidator()

public ReCaptchaValidator(IConfigurationSource configSource)
{
this.configSource = configSource;
this.recaptchaValidator = new RecaptchaValidator();
_configSource = configSource;
_recaptchaValidator = new RecaptchaValidator();
}

/// <summary>
Expand All @@ -32,11 +32,12 @@ public ReCaptchaValidator(IConfigurationSource configSource)
public bool Validate(HttpContextBase httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException("httpContext");
}

var configurationSource = DependencyResolver.Current.GetService<IConfigurationSource>();
var configurationSource = GetConfigurationSource();
var privateApiKey = configurationSource.GetConfigurationValue(Const.ReCaptchaPrivateKeyAppSettingKey);

if (privateApiKey == null)
{
if (!httpContext.Request.IsLocal)
Expand All @@ -53,12 +54,31 @@ public bool Validate(HttpContextBase httpContext)
if (string.IsNullOrWhiteSpace(response))
return false;

recaptchaValidator.PrivateKey = privateApiKey;
recaptchaValidator.RemoteIP = httpContext.Request.UserHostAddress;
recaptchaValidator.Challenge = challenge;
recaptchaValidator.Response = response;
_recaptchaValidator.PrivateKey = privateApiKey;
_recaptchaValidator.RemoteIP = httpContext.Request.UserHostAddress;
_recaptchaValidator.Challenge = challenge;
_recaptchaValidator.Response = response;

return _recaptchaValidator.Validate().IsValid;
}

private IConfigurationSource GetConfigurationSource()
{
if (_configSource != null)
{
return _configSource;
}

if (DependencyResolver.Current != null)
{
var resolvedConfigSource = DependencyResolver.Current.GetService<IConfigurationSource>();
if (resolvedConfigSource != null)
{
return resolvedConfigSource;
}
}

return recaptchaValidator.Validate().IsValid;
return new DefaultConfigurationSource();
}
}
}