示例#1
0
        /// <summary>
        /// <see cref="IHtmlHelper"/> implementation for creating reCAPTCHA components.
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <param name="htmlHelper">The <see cref="IHtmlHelper"/> instance this method extends.</param>
        /// <param name="expression">An expression to be evaluated against the current model.</param>
        /// <param name="type">Type of reCAPTCHA component to be created.</param>
        /// <param name="siteKey">Site Key of the required type. Get keys from the admin console.</param>
        /// <param name="theme">The color theme of the widget.</param>
        /// <param name="size">The size of the widget.</param>
        /// <param name="tabIndex">The tabindex of the widget and challenge. If other elements in your page use tabindex, it should be set to make user navigation easier.</param>
        /// <param name="callback">The name of your Javascript callback function, executed when the user submits a successful response. Response token will be passed to this function.</param>
        /// <param name="expiredCallback">The name of your callback function, executed when the reCAPTCHA response expires and the user needs to re-verify.</param>
        /// <param name="errorCallback">The name of your callback function, executed when reCAPTCHA encounters an error (usually network connectivity) and cannot continue until connectivity is restored. If you specify a function here, you are responsible for informing the user that they should retry.</param>
        /// <param name="action">A name used to provide a detailed break-down of data for your top ten actions in the admin console.</param>
        /// <param name="badge">Controls position and visibility of the reCAPTCHA badge on the page.</param>
        /// <returns>Html and script content to render and execute the reCAPTCHA component.</returns>
        public static IHtmlContent Recaptcha <TModel>(
            this IHtmlHelper <TModel> htmlHelper,
            string expression,
            RecaptchaType type,
            string siteKey,
            ThemeType?theme        = null,
            SizeType?size          = null,
            int?tabIndex           = null,
            string callback        = null,
            string expiredCallback = null,
            string errorCallback   = null,
            string action          = null,
            BadgeType?badge        = null)
        {
            if (htmlHelper is null)
            {
                throw new ArgumentNullException(nameof(htmlHelper));
            }

            TagBuilder input = null;

            if (!string.IsNullOrWhiteSpace(expression))
            {
                input = (TagBuilder)htmlHelper.Hidden(expression);
            }

            var props = new RecaptchaProps(
                type,
                siteKey,
                callback: callback,
                expiredCallback: expiredCallback,
                errorCallback: errorCallback,
                theme: theme,
                tabIndex: tabIndex,
                size: size,
                badge: badge,
                action: action);

            return(props.GenerateHtml(input));
        }
示例#2
0
        public static IHtmlContent RecaptchaV3 <TModel>(
            this IHtmlHelper <TModel> htmlHelper,
            string siteKey,
            string expression   = null,
            string callback     = null,
            string action       = null,
            bool?isBadgeVisible = true,
            BadgeType?badge     = null)
        {
            if (htmlHelper is null)
            {
                throw new ArgumentNullException(nameof(htmlHelper));
            }

            // todo: 'IsBadgeVisible' property is obsolete and should be removed in a future version.
            if (isBadgeVisible.HasValue && !isBadgeVisible.Value)
            {
                // if IsBadgeVisible has been set to false then override Badge property
                badge = BadgeType.None;
            }

            TagBuilder input = null;

            if (!string.IsNullOrWhiteSpace(expression))
            {
                input = (TagBuilder)htmlHelper.Hidden(expression);
            }

            var props = new RecaptchaProps(
                RecaptchaType.V3,
                siteKey,
                callback: callback,
                badge: badge,
                action: action);

            return(props.GenerateHtml(input));
        }
示例#3
0
        public static IHtmlContent RecaptchaV2Checkbox <TModel>(
            this IHtmlHelper <TModel> htmlHelper,
            string siteKey,
            string expression      = null,
            ThemeType?theme        = null,
            SizeType?size          = null,
            int?tabIndex           = null,
            string callback        = null,
            string expiredCallback = null,
            string errorCallback   = null)
        {
            if (htmlHelper is null)
            {
                throw new ArgumentNullException(nameof(htmlHelper));
            }

            TagBuilder input = null;

            if (!string.IsNullOrWhiteSpace(expression))
            {
                // Generate an <input> element using Model or Id property details.
                input = (TagBuilder)htmlHelper.Hidden(expression);
            }

            var props = new RecaptchaProps(
                RecaptchaType.V2Checkbox,
                siteKey,
                callback: callback,
                expiredCallback: expiredCallback,
                errorCallback: errorCallback,
                theme: theme,
                tabIndex: tabIndex,
                size: size);

            return(props.GenerateHtml(input));
        }