Skip to content

Instantly share code, notes, and snippets.

@MoienTajik
Last active February 17, 2018 12:32
Show Gist options
  • Save MoienTajik/52df500c02526017c356056eec7f2f71 to your computer and use it in GitHub Desktop.
Save MoienTajik/52df500c02526017c356056eec7f2f71 to your computer and use it in GitHub Desktop.
Using Google reCAPTCHA in ASP.NET MVC - reCAPTCHA Generator
public static class GoogleCaptchaHelper
{
public static IHtmlString GoogleCaptcha(this HtmlHelper helper)
{
const string publicSiteKey = SiteSettings.GoogleRecaptchaSiteKey;
var mvcHtmlString = new TagBuilder("div")
{
Attributes =
{
new KeyValuePair<string, string>("class", "g-recaptcha"),
new KeyValuePair<string, string>("data-sitekey", publicSiteKey)
}
};
const string googleCaptchaScript = "<script src='https://www.google.com/recaptcha/api.js'></script>";
var renderedCaptcha = mvcHtmlString.ToString(TagRenderMode.Normal);
return MvcHtmlString.Create($"{googleCaptchaScript}{renderedCaptcha}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment