コード例 #1
0
        /// <summary>
        /// Renders the recaptcha HTML in an MVC view. It is an extension method to the <see cref="System.Web.Mvc.HtmlHelper"/> class.
        /// </summary>
        /// <param name="htmlHelper">The <see cref="System.Web.Mvc.HtmlHelper"/> object to which the extension is added.</param>
        /// <param name="siteKey">Sets the site key of recaptcha.</param>
        /// <param name="renderApiScript">Determines if the API script call is to be rendered.</param>
        /// <param name="theme">Sets the theme of recaptcha.</param>
        /// <param name="language">Sets the language of recaptcha. If no language is specified, the language of the current UI culture will be used.</param>
        /// <param name="tabIndex">Sets the tab index of recaptcha.</param>
        /// <param name="size">Sets the size of recaptcha.</param>
        /// <param name="useSsl">Sets the value to the UseSsl property.</param>
        /// <param name="apiVersion">Determines the version of the reCAPTCHA API.</param>
        /// <returns>Returns an instance of the IHtmlString type.</returns>
        public static IHtmlContent RecaptchaWidget(
            this IHtmlHelper htmlHelper,
            string siteKey              = null,
            bool renderApiScript        = true,
            RecaptchaTheme?theme        = null,
            string language             = null,
            int?tabIndex                = null,
            RecaptchaSize?size          = null,
            RecaptchaSslBehavior?useSsl = null,
            string apiVersion           = null)
        {
            var    config = RecaptchaConfigurationManager.GetConfiguration();
            string ver;

            if (!string.IsNullOrEmpty(apiVersion))
            {
                ver = apiVersion;
            }
            else
            {
                ver = config.ApiVersion;
            }

            if (ver == null || ver == "2")
            {
                var rHtmlHelper = new Recaptcha2HtmlHelper(siteKey ?? config.SiteKey);
                return(new HtmlString(rHtmlHelper.CreateWidgetHtml(renderApiScript, theme != null ? (RecaptchaTheme)theme : config.Theme, language ?? config.Language, tabIndex != null ? (int)tabIndex : 0, size != null ? (RecaptchaSize)size : config.Size, useSsl != null ? (RecaptchaSslBehavior)useSsl : config.UseSsl)));
            }
            else
            {
                throw new InvalidOperationException("The API version is either invalid or not supported.");
            }
        }
コード例 #2
0
        /// <summary>
        /// Renders the recaptcha HTML in an MVC view. It is an extension method to the <see cref="System.Web.Mvc.HtmlHelper"/> class.
        /// </summary>
        /// <param name="htmlHelper">The <see cref="System.Web.Mvc.HtmlHelper"/> object to which the extension is added.</param>
        /// <param name="siteKey">Sets the site key of recaptcha.</param>
        /// <param name="language">Sets the language of recaptcha. If no language is specified, the language of the current UI culture will be used.</param>
        /// <param name="useSsl">Sets the value to the UseSsl property.</param>
        /// <param name="apiVersion">Determines the version of the reCAPTCHA API.</param>
        /// <returns>Returns an instance of the IHtmlString type.</returns>
        public static IHtmlContent RecaptchaApiScript(
            this IHtmlHelper htmlHelper,
            string siteKey              = null,
            string language             = null,
            RecaptchaSslBehavior?useSsl = null,
            string apiVersion           = null)
        {
            var    config = RecaptchaConfigurationManager.GetConfiguration();
            string ver;

            if (!string.IsNullOrEmpty(apiVersion))
            {
                ver = apiVersion;
            }
            else
            {
                ver = config.ApiVersion;
            }

            if (ver == null || ver == "2")
            {
                var rHtmlHelper = new Recaptcha2HtmlHelper(siteKey ?? config.SiteKey);
                return(new HtmlString(rHtmlHelper.CreateApiScripttHtml(language ?? config.Language, useSsl != null ? (RecaptchaSslBehavior)useSsl : config.UseSsl)));
            }
            else
            {
                throw new InvalidOperationException("The API version is either invalid or not supported.");
            }
        }
コード例 #3
0
        /// <summary>
        /// Verifies whether the user's response to the recaptcha request is correct.
        /// </summary>
        /// <returns>Returns the result as a value of the <see cref="RecaptchaVerificationResult"/> enum.</returns>
        public RecaptchaVerificationResult VerifyRecaptchaResponse()
        {
            if (string.IsNullOrEmpty(Response))
            {
                return(new RecaptchaVerificationResult {
                    Success = false
                });
            }

            string secretKey = SecretKey;

            if (string.IsNullOrEmpty(secretKey))
            {
                var config = RecaptchaConfigurationManager.GetConfiguration();
                secretKey = config.SecretKey;
            }

            return(VerifyRecpatcha2Response(secretKey));
        }
コード例 #4
0
        /// <summary>
        /// Verifies whether the user's response to the recaptcha request is correct.
        /// </summary>
        /// <returns>Returns the result as a value of the <see cref="RecaptchaVerificationResult"/> enum.</returns>
        public Task <RecaptchaVerificationResult> VerifyRecaptchaResponseTaskAsync()
        {
            if (string.IsNullOrEmpty(Response))
            {
                Task <RecaptchaVerificationResult> .Factory.StartNew(() =>
                {
                    return(new RecaptchaVerificationResult {
                        Success = false
                    });
                });
            }

            string secretKey = SecretKey;

            if (string.IsNullOrEmpty(secretKey))
            {
                var config = RecaptchaConfigurationManager.GetConfiguration();
                secretKey = config.SecretKey;
            }

            return(VerifyRecpatcha2ResponseTaskAsync(secretKey));
        }
コード例 #5
0
        /// <summary>
        /// Gets an instance of the <see cref="RecaptchaVerificationHelper"/> class that can be used to verify user's response to the recaptcha's challenge.
        /// </summary>
        /// <param name="controller">The <see cref="System.Web.Mvc.Controller"/> object to which the extension method is added to.</param>
        /// <returns>Returns an instance of the <see cref="RecaptchaVerificationHelper"/> class.</returns>
        public static RecaptchaVerificationHelper GetRecaptchaVerificationHelper(this Controller controller)
        {
            var config = RecaptchaConfigurationManager.GetConfiguration();

            return(new RecaptchaVerificationHelper(controller.HttpContext, config.SecretKey));
        }
コード例 #6
0
        /// <summary>
        /// Gets an instance of the <see cref="RecaptchaVerificationHelper"/> class that can be used to verify user's response to the recaptcha's challenge.
        /// </summary>
        /// <param name="controller">The <see cref="System.Web.Mvc.Controller"/> object to which the extension method is added to.</param>
        /// <returns>Returns an instance of the <see cref="RecaptchaVerificationHelper"/> class.</returns>
        public static RecaptchaVerificationHelper GetRecaptchaVerificationHelper(this System.Web.Mvc.Controller controller)
        {
            var config = RecaptchaConfigurationManager.GetConfiguration();

            return(new RecaptchaVerificationHelper(config.SecretKey));
        }