Exemplo n.º 1
0
 /// <summary>
 /// Creates an instance of the <see cref="RecaptchaConfiguration"/> class.
 /// </summary>
 /// <param name="siteKey">The site key.</param>
 /// <param name="secretKey">The secret key.</param>
 /// <param name="apiVersion">The API version of reCAPTCHA to be used.</param>
 /// <param name="theme">The color theme of the widget.</param>
 /// <param name="language">Forces the reCAPTCHA widget to render in a specific language. By default, the user's language is used.</param>
 /// <param name="size">The size of the reCAPTCHA widget.</param>
 /// <param name="useSsl">Determines if SSL is to be used in Google reCAPTCHA API calls.</param>
 ///
 public RecaptchaConfiguration(string siteKey, string secretKey, string apiVersion, string language = null, RecaptchaTheme theme = RecaptchaTheme.Default, RecaptchaSize size = RecaptchaSize.Default, RecaptchaSslBehavior useSsl = RecaptchaSslBehavior.AlwaysUseSsl)
 {
     SiteKey    = siteKey;
     SecretKey  = secretKey;
     ApiVersion = apiVersion;
     Language   = language;
     Theme      = theme;
     Size       = size;
     UseSsl     = useSsl;
 }
        /// <summary>
        /// Gets the configuration from the default source.
        /// </summary>
        /// <returns>Returns configuration as an instance of the <see cref="RecaptchaConfiguration"/> class.</returns>
        public static RecaptchaConfiguration GetConfiguration()
        {
            string               siteKey    = _configuration["RecaptchaSiteKey"];
            string               secretKey  = _configuration["RecaptchaSecretKey"];
            string               language   = _configuration["RecaptchaLanguage"];
            string               apiVersion = _configuration["RecaptchaApiVersion"] ?? "2";
            RecaptchaSize        size       = _configuration["RecaptchaSize"] == null ? RecaptchaSize.Default : Enum.Parse <RecaptchaSize>(_configuration["RecaptchaSize"]);
            RecaptchaTheme       theme      = _configuration["RecaptchaTheme"] == null ? RecaptchaTheme.Default : Enum.Parse <RecaptchaTheme>(_configuration["RecaptchaTheme"]);
            RecaptchaSslBehavior useSsl     = _configuration["RecaptchaUseSsl"] == null ? RecaptchaSslBehavior.AlwaysUseSsl : Enum.Parse <RecaptchaSslBehavior>(_configuration["RecaptchaUseSsl"]);

            return(new RecaptchaConfiguration(siteKey, secretKey, apiVersion, language, theme, size, useSsl));
        }
        /// <summary>
        /// Gets the configuration from the default source.
        /// </summary>
        /// <returns>Returns configuration as an instance of the <see cref="RecaptchaConfiguration"/> class.</returns>
        public static RecaptchaConfiguration GetConfiguration()
        {
            string               siteKey = null, secretKey = null, language = null, apiVersion = "2";
            RecaptchaSize        size   = RecaptchaSize.Default;
            RecaptchaTheme       theme  = RecaptchaTheme.Default;
            RecaptchaSslBehavior useSsl = RecaptchaSslBehavior.AlwaysUseSsl;

            if (ConfigurationManager.AppSettings.AllKeys.Contains("RecaptchaSiteKey"))
            {
                siteKey = ConfigurationManager.AppSettings["RecaptchaSiteKey"];
            }

            if (ConfigurationManager.AppSettings.AllKeys.Contains("RecaptchaSecretKey"))
            {
                secretKey = ConfigurationManager.AppSettings["RecaptchaSecretKey"];
            }

            if (ConfigurationManager.AppSettings.AllKeys.Contains("RecaptchaApiVersion"))
            {
                apiVersion = ConfigurationManager.AppSettings["RecaptchaApiVersion"];
            }

            if (ConfigurationManager.AppSettings.AllKeys.Contains("RecaptchaLanguage"))
            {
                language = ConfigurationManager.AppSettings["RecaptchaLanguage"];
            }

            if (ConfigurationManager.AppSettings.AllKeys.Contains("RecaptchaTheme"))
            {
                Enum.TryParse <RecaptchaTheme>(ConfigurationManager.AppSettings["RecaptchaTheme"], out theme);
            }

            if (ConfigurationManager.AppSettings.AllKeys.Contains("RecaptchaSize"))
            {
                Enum.TryParse <RecaptchaSize>(ConfigurationManager.AppSettings["RecaptchaSize"], out size);
            }

            if (ConfigurationManager.AppSettings.AllKeys.Contains("RecaptchaUseSsl"))
            {
                Enum.TryParse <RecaptchaSslBehavior>(ConfigurationManager.AppSettings["RecaptchaUseSsl"], out useSsl);
            }

            return(new RecaptchaConfiguration(siteKey, secretKey, apiVersion, language, theme, size, useSsl));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates the reCAPTCHA HTML that needs to be rendered.
        /// </summary>
        /// <param name="renderApiScript">Determines if the API script is to be rendered.</param>
        /// <param name="theme">The color theme of the widget.</param>
        /// <param name="language">Forces the reCAPTCHA widget to render in a specific language. By default, the user's language is used.</param>
        /// <param name="tabIndex">The tabindex of the reCAPTCHA widget.</param>
        /// <param name="size">The size of the reCAPTCHA widget.</param>
        /// <param name="useSsl">Determines if SSL is to be used in Google reCAPTCHA API calls.</param>
        /// <returns>Returns the reCAPTCHA HTML as an instance of the <see cref="string"/> type.</returns>
        public string CreateWidgetHtml(bool renderApiScript, RecaptchaTheme theme, string language, int tabIndex, RecaptchaSize size, RecaptchaSslBehavior useSsl)
        {
            var dictAttributes = new Dictionary <string, string>
            {
                { "data-" + PARAM_SITEKEY, SiteKey }
            };

            if (theme != RecaptchaTheme.Default)
            {
                dictAttributes.Add("data-" + PARAM_THEME, theme.ToString().ToLower());
            }

            if (tabIndex != 0)
            {
                dictAttributes.Add("data-" + PARAM_TABINDEX, tabIndex.ToString());
            }

            if (size != RecaptchaSize.Default)
            {
                dictAttributes.Add("data-" + PARAM_SIZE, size.ToString().ToLower());
            }

            var sbAttributes = new StringBuilder();

            foreach (var key in dictAttributes.Keys)
            {
                sbAttributes.Append($"{key}=\"{dictAttributes[key]}\" ");
            }

            StringBuilder sbHtml = new StringBuilder();

            if (renderApiScript)
            {
                sbHtml.Append(CreateApiScripttHtml(language, useSsl));
            }

            sbHtml.Append($"<div class=\"g-recaptcha\" {sbAttributes.ToString()}></div>");

            return(sbHtml.ToString());
        }