/// <summary>
        /// Renders the reCAPTCHA HTML in an MVC view.
        /// </summary>
        /// <param name="htmlHelper">The <see cref="System.Web.Mvc.HtmlHelper"/> object to which the extension is added.</param>
        /// <param name="publicKey">Sets the public key (Site key) used to render the reCAPTCHA HTML.</param>
        /// <param name="colorTheme">Sets the color theme used to render the reCAPTCHA HTML.
        /// <para/> If null, no color theme will be specified and light theme should be used by the reCAPTCHA API.</param>
        /// <param name="language">Sets the language used to render the reCAPTCHA HTML.
        /// <para/> If null, no language will be specified and the language should be detected by the reCAPTCHA API.</param>
        /// <returns>Returns rendered reCAPTCHA HTML as <see cref="IHtmlString"/>.</returns>
        public static IHtmlString RecaptchaWidget(
            this HtmlHelper htmlHelper,
            string publicKey      = "{recaptchaPublicKey}",
            ColorTheme?colorTheme = null,
            string language       = null)
        {
            var recaptchaHtmlHelper = new RecaptchaHtmlHelper(publicKey);

            if (colorTheme.HasValue)
            {
                recaptchaHtmlHelper.ColorTheme = colorTheme.Value;
            }

            if (String.IsNullOrEmpty(language) == false)
            {
                recaptchaHtmlHelper.Language = language;
            }

            using (var stringWriter = new StringWriter())
            {
                using (var htmlWriter = new HtmlTextWriter(stringWriter))
                {
                    recaptchaHtmlHelper.Render(htmlWriter);
                }

                return(new MvcHtmlString(stringWriter.ToString()));
            }
        }
Example #2
0
 /// <summary>
 /// Renders the reCAPTCHA widget HTML. This method is automatically called by ASP.NET during the rendering process.
 /// </summary>
 /// <param name="output">The output writer, the HTML will writen to.</param>
 protected override void RenderContents(HtmlTextWriter output)
 {
     if (this.DesignMode)
     {
         output.RenderBeginTag(HtmlTextWriterTag.P);
         output.Write("reCAPTCHA widget");
         output.RenderEndTag();
     }
     else
     {
         var htmlHelper = new RecaptchaHtmlHelper(this.PublicKey)
         {
             ColorTheme = ColorTheme,
             Language   = Language
         };
         htmlHelper.Render(output);
     }
 }
        public static IHtmlString Recaptcha(
            this HtmlHelper htmlHelper,
            string publicKey = "{recaptchaPublicKey}",
#pragma warning disable 618
            RecaptchaTheme theme = RecaptchaTheme.Red,
#pragma warning restore 618
            string language = null,
            int tabIndex    = 0,
            bool useSsl     = false)
        {
            var recaptchaHtmlHelper = new RecaptchaHtmlHelper(publicKey, theme, language, tabIndex);

            using (var stringWriter = new StringWriter())
            {
                using (var htmlWriter = new HtmlTextWriter(stringWriter))
                {
                    recaptchaHtmlHelper.Render(htmlWriter);
                }

                return(new MvcHtmlString(stringWriter.ToString()));
            }
        }