示例#1
0
        /// <summary>
        /// The setup recaptcha control.
        /// </summary>
        private void SetupRecaptchaControl()
        {
            this.CreateUserWizard1.FindWizardControlRecursive("RecaptchaPlaceHolder").Visible = true;
            if (string.IsNullOrEmpty(this.PageContext.BoardSettings.RecaptchaPrivateKey) ||
                string.IsNullOrEmpty(this.PageContext.BoardSettings.RecaptchaPrivateKey))
            {
                // this.PageContext.AddLoadMessage(this.GetText("RECAPTCHA_BADSETTING"));
                DB.eventlog_create(this.PageContext.PageUserID, this, "Private or public key for Recapture required!");
                YafBuildLink.AccessDenied();
            }

            this.Recupt = new RecaptchaControl
            {
                ID                     = "Recaptcha1",
                PrivateKey             = this.PageContext.BoardSettings.RecaptchaPrivateKey,
                PublicKey              = this.PageContext.BoardSettings.RecaptchaPublicKey,
                AllowMultipleInstances = this.PageContext.BoardSettings.RecaptureMultipleInstances,
                Enabled                = true,
                EnableTheming          = true,
                // 'red' , 'white', 'blackglass' , 'clean' , 'custom'
                Theme = "blackglass",
                OverrideSecureMode = false
            };

            this.recPH = (PlaceHolder)this.CreateUserWizard1.FindWizardControlRecursive("RecaptchaControl");
            this.recPH.Controls.Add(this.Recupt);
            this.recPH.Visible = true;
        }
示例#2
0
        /// <summary>
        /// Html Helper to build and render the Captcha control
        /// </summary>
        /// <param name="helper">HtmlHelper class provides a set of helper methods whose purpose is to help you create HTML controls programmatically</param>
        /// <returns></returns>
        public static string GenerateCaptcha(this HtmlHelper helper)
        {
            var captchaControl = new RecaptchaControl {
                ID         = "recaptcha",
                Theme      = "clean", //http://wiki.recaptcha.net/index.php/Theme
                PublicKey  = ConfigurationManager.AppSettings["ReCaptchaPublicKey"],
                PrivateKey = ConfigurationManager.AppSettings["ReCaptchaPrivateKey"]
            };
            var htmlWriter = new HtmlTextWriter(new StringWriter());

            captchaControl.RenderControl(htmlWriter);
            return(htmlWriter.InnerWriter.ToString());
        }
示例#3
0
        /// <summary>
        /// Generates CAPTCHA HTML using reCAPTCHA.
        /// </summary>
        /// <param name="htmlHelper">The view's HTML helper.</param>
        /// <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,
            string fallbackMessage = null)
        {
            if (htmlHelper == null)
            {
                throw new ArgumentNullException("htmlHelper");
            }

            IConfigurationSource configurationSource = GetConfigurationSource();
            var publicApiKey = configurationSource.GetConfigurationValue(Const.ReCaptchaPublicKeyAppSettingKey);

            if (publicApiKey == null)
            {
                if (!htmlHelper.ViewContext.HttpContext.Request.IsLocal)
                {
                    throw new InvalidOperationException(ErrorMessage.DefaultReCaptchApiKeysOnlyAllowedForLocalRequest);
                }

                publicApiKey = Const.ReCaptchaLocalhostPublicKey;
            }

            var privateApiKey = configurationSource.GetConfigurationValue(Const.ReCaptchaPrivateKeyAppSettingKey);

            if (privateApiKey == null)
            {
                if (!htmlHelper.ViewContext.HttpContext.Request.IsLocal)
                {
                    throw new InvalidOperationException(ErrorMessage.DefaultReCaptchApiKeysOnlyAllowedForLocalRequest);
                }

                privateApiKey = Const.ReCaptchaLocalhostPrivateKey;
            }

            var recaptchaControl = new RecaptchaControl
            {
                ID         = Const.ReCaptchControlId,
                PublicKey  = publicApiKey,
                PrivateKey = privateApiKey,
            };

            var htmlWriter = new HtmlTextWriter(new StringWriter());

            recaptchaControl.RenderControl(htmlWriter);

            var captchaHtml = htmlWriter.InnerWriter.ToString();

            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)));
        }
        public static MvcHtmlString GenerateCaptcha(this HtmlHelper helper)
        {
            var control = new RecaptchaControl
            {
                ID         = "recaptcha",
                Theme      = "clean",
                PublicKey  = ConfigurationManager.AppSettings["ReCaptchaPublicKey"],
                PrivateKey = ConfigurationManager.AppSettings["ReCaptchaPrivateKey"]
            };
            var writer = new HtmlTextWriter(new StringWriter());

            control.RenderControl(writer);
            var html = writer.InnerWriter.ToString();

            return(MvcHtmlString.Create(html));
        }
示例#5
0
        public static string GenerateCaptcha(this HtmlHelper helper)
        {
            var captchaSettings = EngineContext.Current.Resolve <CaptchaSettings>();
            var captchaControl  = new RecaptchaControl {
                ID         = "recaptcha",
                Theme      = "blackglass",
                PublicKey  = captchaSettings.ReCaptchaPublicKey,
                PrivateKey = captchaSettings.ReCaptchaPrivateKey
            };

            var htmlWriter = new HtmlTextWriter(new StringWriter());

            captchaControl.RenderControl(htmlWriter);

            return(htmlWriter.InnerWriter.ToString());
        }
示例#6
0
        public static MvcHtmlString Captcha(this HtmlHelper helper, string publicKey, string privateKey, string id, string theme)
        {
            var captcha = new RecaptchaControl
            {
                ID         = id,
                PublicKey  = publicKey,
                PrivateKey = privateKey,
                Theme      = theme
            };

            var htmlWriter = new HtmlTextWriter(new StringWriter());

            captcha.RenderControl(htmlWriter);

            return(new MvcHtmlString(htmlWriter.InnerWriter.ToString()));
        }
        public static RecaptchaControl NewRecaptchControl()
        {
            var control = new RecaptchaControl
            {
                PrivateKey = AppSettings.RecaptchaPrivateKey,
                PublicKey  = AppSettings.RecaptchaPublicKey,
                Theme      = "clean"
            };

            if (!Context.Request.IsSecureConnection)
            {
                var forwarded = Context.Request.Headers["X-Forwarded-Proto"];
                control.OverrideSecureMode = forwarded != null && forwarded.StartsWith("https");
            }

            return(control);
        }
示例#8
0
        public static string GenerateReCaptcha(this HtmlHelper helper, string id, string theme)
        {
            if (string.IsNullOrEmpty(Settings.ReCaptchaPublicKey) || string.IsNullOrEmpty(Settings.ReCaptchaPrivateKey))
            {
                throw new ApplicationException("reCAPTCHA needs to be configured with a public & private key.");
            }
            RecaptchaControl recaptchaControl1 = new RecaptchaControl();

            recaptchaControl1.ID         = id;
            recaptchaControl1.Theme      = theme;
            recaptchaControl1.PublicKey  = Settings.ReCaptchaPublicKey;
            recaptchaControl1.PrivateKey = Settings.ReCaptchaPrivateKey;
            RecaptchaControl recaptchaControl2 = recaptchaControl1;
            HtmlTextWriter   writer            = new HtmlTextWriter(new StringWriter());

            recaptchaControl2.RenderControl(writer);
            return(writer.InnerWriter.ToString());
        }
示例#9
0
        public static string GenerateCaptcha(this HtmlHelper helper)
        {
            var captchaSettings = EngineContext.Current.Resolve <CaptchaSettings>();

            var theme          = !string.IsNullOrEmpty(captchaSettings.ReCaptchaTheme) ? captchaSettings.ReCaptchaTheme : "white";
            var captchaControl = new RecaptchaControl
            {
                ID         = "recaptcha",
                Theme      = theme,
                PublicKey  = captchaSettings.ReCaptchaPublicKey,
                PrivateKey = captchaSettings.ReCaptchaPrivateKey
            };

            var htmlWriter = new HtmlTextWriter(new StringWriter());

            captchaControl.RenderControl(htmlWriter);

            return(htmlWriter.InnerWriter.ToString());
        }