コード例 #1
0
 /// <summary>
 /// Creates an instance of the <see cref="Recaptcha2HtmlHelper"/> class.
 /// </summary>
 /// <param name="publicKey">Sets the public key of the recaptcha HTML.</param>
 /// <param name="theme">Sets the theme of the recaptcha HTML.</param>
 /// <param name="language">Sets the language of the recaptcha HTML.</param>
 /// <param name="tabIndex">Sets the tab index of the recaptcha HTML.</param>
 /// <param name="dataType">Sets the type of the recaptcha HTML.</param>
 /// <param name="dataSize">Sets the size for the recpatcha HTML.</param>
 /// <param name="useSsl">Determines whether to use SSL in reCAPTCHA API URLs.</param>
 /// <param name="withCallback">Determines if we need a callback for our reCaptcha</param>
 public Recaptcha2HtmlHelper(string publicKey, RecaptchaTheme theme, string language, int tabIndex, RecaptchaDataType?dataType, RecaptchaDataSize?dataSize, SslBehavior useSsl, bool withCallback, string callBack)
     : base(publicKey, theme, language, tabIndex, useSsl, withCallback)
 {
     DataType     = dataType;
     DataSize     = dataSize;
     CallbackName = callBack;
 }
コード例 #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="publicKey">Sets the public key of recaptcha.</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="dataType">Sets the data type of recaptcha.</param>
        /// <param name="dataSize">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 IHtmlString Recaptcha(
            this HtmlHelper htmlHelper,
            string publicKey = "{recaptchaPublicKey}",
            RecaptchaTheme theme = RecaptchaTheme.Red,
            string language = null,
            int tabIndex = 0,
            RecaptchaDataType? dataType = null,
            RecaptchaDataSize? dataSize = null,
            SslBehavior useSsl = SslBehavior.SameAsRequestUrl,
            string apiVersion = "{recaptchaApiVersion}")
        {
            IRecaptchaHtmlHelper rHtmlHelper = null;

            string version = RecaptchaKeyHelper.ParseKey(apiVersion);

            if (version != "2")
            {
                rHtmlHelper = new RecaptchaHtmlHelper(publicKey, theme, language, tabIndex, useSsl);
            }
            else
            {
                rHtmlHelper = new Recaptcha2HtmlHelper(publicKey, theme, language, tabIndex, dataType, dataSize, useSsl);
            }

            var writer = new HtmlTextWriter(new StringWriter());
            writer.Write(rHtmlHelper.ToString());

            return htmlHelper.Raw(writer.InnerWriter.ToString());
        }
コード例 #3
0
 /// <summary>
 /// Creates an instance of the <see cref="Recaptcha2HtmlHelper"/> class.
 /// </summary>
 /// <param name="publicKey">Sets the public key of the recaptcha HTML.</param>
 /// <param name="theme">Sets the theme of the recaptcha HTML.</param>
 /// <param name="language">Sets the language of the recaptcha HTML.</param>
 /// <param name="tabIndex">Sets the tab index of the recaptcha HTML.</param>
 /// <param name="dataType">Sets the type of the recaptcha HTML.</param>
 /// <param name="dataSize">Sets the size for the recpatcha HTML.</param>
 /// <param name="useSsl">Determines whether to use SSL in reCAPTCHA API URLs.</param>
 /// <param name="dataCallback">Sets the data-callback property of the recaptcha HTML.</param>
 /// <param name="dataExpiredCallback">Sets the data-expired-callback property of the recaptcha HTML.</param>
 public Recaptcha2HtmlHelper(string publicKey, RecaptchaTheme theme, string language, int tabIndex, RecaptchaDataType?dataType, RecaptchaDataSize?dataSize, SslBehavior useSsl, string dataCallback, string dataExpiredCallback)
     : base(publicKey, theme, language, tabIndex, useSsl, dataCallback, dataExpiredCallback)
 {
     DataType            = dataType;
     DataSize            = dataSize;
     DataCallback        = dataCallback;
     DataExpiredCallback = dataExpiredCallback;
 }
コード例 #4
0
        public RecaptchaHtmlHelper(string publicKey, RecaptchaTheme theme, string language, int tabIndex)
            : this(publicKey)
        {
            this.Language = language;
#pragma warning disable 618
            this.TabIndex = tabIndex;
            this.Theme    = theme;
#pragma warning restore 618
        }
コード例 #5
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;
 }
コード例 #6
0
        /// <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));
        }
コード例 #7
0
        /// <summary>
        /// Creates an instance of the <see cref="RecaptchaHtmlHelper"/> class.
        /// </summary>
        /// <param name="publicKey">Sets the public key of the recaptcha HTML.</param>
        /// <param name="theme">Sets the theme of the recaptcha HTML.</param>
        /// <param name="language">Sets the language of the recaptcha HTML.</param>
        /// <param name="tabIndex">Sets the tab index of the recaptcha HTML.</param>    
        public RecaptchaHtmlHelper(string publicKey, RecaptchaTheme theme, string language, int tabIndex)
        {
            this.PublicKey = RecaptchaKeyHelper.ParseKey(publicKey);

            if (String.IsNullOrEmpty(this.PublicKey))
            {
                throw new InvalidOperationException("Public key cannot be null or empty.");
            }

            this.Theme = theme;
            this.Language = language;
            this.TabIndex = tabIndex;
        }
コード例 #8
0
        /// <summary>
        /// Creates an instance of the <see cref="RecaptchaHtmlHelper"/> class.
        /// </summary>
        /// <param name="publicKey">Sets the public key of the recaptcha HTML.</param>
        /// <param name="theme">Sets the theme of the recaptcha HTML.</param>
        /// <param name="language">Sets the language of the recaptcha HTML.</param>
        /// <param name="tabIndex">Sets the tab index of the recaptcha HTML.</param>    
        public RecaptchaHtmlHelper(string publicKey, RecaptchaTheme theme, string language, int tabIndex)
        {
            this.PublicKey = RecaptchaKeyHelper.ParseKey(publicKey);

            if (String.IsNullOrEmpty(this.PublicKey))
            {
                throw new InvalidOperationException("Public key cannot be null or empty.");
            }

            this.Theme = theme;
            this.Language = language;
            this.TabIndex = tabIndex;

            UseSsl = HttpContext.Current.Request.IsSecureConnection;
        }
コード例 #9
0
        /// <summary>
        /// Creates an instance of the <see cref="RecaptchaHtmlHelper"/> class.
        /// </summary>
        /// <param name="publicKey">Sets the public key of the recaptcha HTML.</param>
        /// <param name="theme">Sets the theme of the recaptcha HTML.</param>
        /// <param name="language">Sets the language of the recaptcha HTML.</param>
        /// <param name="tabIndex">Sets the tab index of the recaptcha HTML.</param>
        public RecaptchaHtmlHelper(string publicKey, RecaptchaTheme theme, string language, int tabIndex)
        {
            this.PublicKey = RecaptchaKeyHelper.ParseKey(publicKey);

            if (String.IsNullOrEmpty(this.PublicKey))
            {
                throw new InvalidOperationException("Public key cannot be null or empty.");
            }

            this.Theme    = theme;
            this.Language = language;
            this.TabIndex = tabIndex;

            UseSsl = HttpContext.Current.Request.Url.AbsoluteUri.StartsWith("https");
        }
コード例 #10
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="publicKey">Sets the public key of recaptcha.</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="useSsl">Sets the value to the UseSsl property.</param>
        /// <returns>Returns an instance of the IHtmlString type.</returns>
        public static IHtmlString Recaptcha(
            this HtmlHelper htmlHelper,
            string publicKey = "{recaptchaPublicKey}",
            RecaptchaTheme theme = RecaptchaTheme.Red,
            string language = null,
            int tabIndex = 0,
            bool useSsl = false)
        {
            RecaptchaHtmlHelper rHtmlHelper = new RecaptchaHtmlHelper(publicKey, theme, language, tabIndex, useSsl);

            HtmlTextWriter writer = new HtmlTextWriter(new StringWriter());
            writer.Write(rHtmlHelper.ToString());

            return htmlHelper.Raw(writer.InnerWriter.ToString());
        }
コード例 #11
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="publicKey">Sets the public key of recaptcha.</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="useSsl">Sets the value to the UseSsl property.</param>
        /// <returns>Returns an instance of the IHtmlString type.</returns>
        public static IHtmlString Recaptcha(
            this HtmlHelper htmlHelper,
            string publicKey     = "{recaptchaPublicKey}",
            RecaptchaTheme theme = RecaptchaTheme.Red,
            string language      = null,
            int tabIndex         = 0,
            bool useSsl          = false)
        {
            RecaptchaHtmlHelper rHtmlHelper = new RecaptchaHtmlHelper(publicKey, theme, language, tabIndex);

            HtmlTextWriter writer = new HtmlTextWriter(new StringWriter());

            writer.Write(rHtmlHelper.ToString());

            return(htmlHelper.Raw(writer.InnerWriter.ToString()));
        }
コード例 #12
0
        /// <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));
        }
コード例 #13
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());
        }
コード例 #14
0
        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()));
            }
        }
コード例 #15
0
 /// <summary>
 /// Creates an instance of the <see cref="RecaptchaHtmlHelper"/> class.
 /// </summary>
 /// <param name="publicKey">Sets the public key of the recaptcha HTML.</param>
 /// <param name="theme">Sets the theme of the recaptcha HTML.</param>
 /// <param name="language">Sets the language of the recaptcha HTML.</param>
 /// <param name="tabIndex">Sets the tab index of the recaptcha HTML.</param>    
 /// <param name="useSsl">Determines whether to use SSL in reCAPTCHA API URLs.</param>
 public RecaptchaHtmlHelper(string publicKey, RecaptchaTheme theme, string language, int tabIndex, SslBehavior useSsl)
     : base(publicKey, theme, language, tabIndex, useSsl)
 { }
コード例 #16
0
 /// <summary>
 /// Creates an instance of the <see cref="Recaptcha2HtmlHelper"/> class.
 /// </summary>
 /// <param name="publicKey">Sets the public key of the recaptcha HTML.</param>
 /// <param name="theme">Sets the theme of the recaptcha HTML.</param>
 /// <param name="language">Sets the language of the recaptcha HTML.</param>
 /// <param name="tabIndex">Sets the tab index of the recaptcha HTML.</param>   
 public Recaptcha2HtmlHelper(string publicKey, RecaptchaTheme theme, string language, int tabIndex)
     : base(publicKey, theme, language, tabIndex)
 {
     DataType = null;
     DataSize = null;
 }
コード例 #17
0
 /// <summary>
 /// Creates an instance of the <see cref="Recaptcha2HtmlHelper"/> class.
 /// </summary>
 /// <param name="publicKey">Sets the public key of the recaptcha HTML.</param>
 /// <param name="theme">Sets the theme of the recaptcha HTML.</param>
 /// <param name="language">Sets the language of the recaptcha HTML.</param>
 /// <param name="tabIndex">Sets the tab index of the recaptcha HTML.</param>
 /// <param name="dataType">Sets the type of the recaptcha HTML.</param>
 /// <param name="dataSize">Sets the size for the recpatcha HTML.</param>
 /// <param name="useSsl">Determines whether to use SSL in reCAPTCHA API URLs.</param>
 public Recaptcha2HtmlHelper(string publicKey, RecaptchaTheme theme, string language, int tabIndex, RecaptchaDataType?dataType, RecaptchaDataSize?dataSize, SslBehavior useSsl)
     : base(publicKey, theme, language, tabIndex, useSsl)
 {
     DataType = dataType;
     DataSize = dataSize;
 }
コード例 #18
0
 /// <summary>
 /// Creates an instance of the <see cref="Recaptcha2HtmlHelper"/> class.
 /// </summary>
 /// <param name="publicKey">Sets the public key of the recaptcha HTML.</param>
 /// <param name="theme">Sets the theme of the recaptcha HTML.</param>
 /// <param name="language">Sets the language of the recaptcha HTML.</param>
 /// <param name="tabIndex">Sets the tab index of the recaptcha HTML.</param>
 public Recaptcha2HtmlHelper(string publicKey, RecaptchaTheme theme, string language, int tabIndex)
     : base(publicKey, theme, language, tabIndex)
 {
     DataType = null;
     DataSize = null;
 }
コード例 #19
0
 /// <summary>
 /// Creates an instance of the <see cref="Recaptcha2HtmlHelper"/> class.
 /// </summary>
 /// <param name="publicKey">Sets the public key of the recaptcha HTML.</param>
 /// <param name="theme">Sets the theme of the recaptcha HTML.</param>
 /// <param name="language">Sets the language of the recaptcha HTML.</param>
 /// <param name="tabIndex">Sets the tab index of the recaptcha HTML.</param>    
 /// <param name="dataType">Sets the type of the recaptcha HTML.</param>
 /// <param name="dataSize">Sets the size for the recpatcha HTML.</param>
 /// <param name="useSsl">Determines whether to use SSL in reCAPTCHA API URLs.</param>
 public Recaptcha2HtmlHelper(string publicKey, RecaptchaTheme theme, string language, int tabIndex, RecaptchaDataType? dataType, RecaptchaDataSize? dataSize, SslBehavior useSsl)
     : base(publicKey, theme, language, tabIndex, useSsl)
 {
     DataType = dataType;
     DataSize = dataSize;
 }
コード例 #20
0
 /// <summary>
 /// Creates an instance of the <see cref="RecaptchaHtmlHelper"/> class.
 /// </summary>
 /// <param name="publicKey">Sets the public key of the recaptcha HTML.</param>
 /// <param name="theme">Sets the theme of the recaptcha HTML.</param>
 /// <param name="language">Sets the language of the recaptcha HTML.</param>
 /// <param name="tabIndex">Sets the tab index of the recaptcha HTML.</param>
 /// <param name="useSsl">Determines whether to use SSL in reCAPTCHA API URLs.</param>
 public RecaptchaHtmlHelper(string publicKey, RecaptchaTheme theme, string language, int tabIndex, SslBehavior useSsl)
     : base(publicKey, theme, language, tabIndex, useSsl)
 {
 }
コード例 #21
0
 /// <summary>
 /// Creates an instance of the <see cref="RecaptchaHtmlHelper"/> class.
 /// </summary>
 /// <param name="publicKey">Sets the public key of the recaptcha HTML.</param>
 /// <param name="theme">Sets the theme of the recaptcha HTML.</param>
 /// <param name="language">Sets the language of the recaptcha HTML.</param>
 /// <param name="tabIndex">Sets the tab index of the recaptcha HTML.</param>
 public RecaptchaHtmlHelper(string publicKey, RecaptchaTheme theme, string language, int tabIndex)
     : base(publicKey, theme, language, tabIndex)
 {
 }
コード例 #22
0
 /// <summary>
 /// Creates an instance of the <see cref="RecaptchaHtmlHelper"/> class.
 /// </summary>
 /// <param name="publicKey">Sets the public key of the recaptcha HTML.</param>
 /// <param name="theme">Sets the theme of the recaptcha HTML.</param>
 /// <param name="language">Sets the language of the recaptcha HTML.</param>
 /// <param name="tabIndex">Sets the tab index of the recaptcha HTML.</param>   
 public RecaptchaHtmlHelper(string publicKey, RecaptchaTheme theme, string language, int tabIndex)
     : base(publicKey, theme, language, tabIndex)
 { }