public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            output.TagMode = TagMode.StartTagAndEndTag;
            output.TagName = null;

            if (string.IsNullOrEmpty(Id))
            {
                throw new ArgumentException("id can't be null");
            }

            if (Id.ToLower() == "submit")
            {
                throw new ArgumentException("id can't be named submit");
            }

            Language ??= ViewContext.HttpContext.Features.Get <IRequestCultureFeature>()?.RequestCulture.UICulture.TwoLetterISOLanguageName;

            var settings = ViewContext.HttpContext.RequestServices.GetRequiredService <IOptions <ReCaptchaSettings> >().Value;

            var content = settings.Version switch
            {
                ReCaptchaVersion.V2Invisible => ReCaptchaGenerator.ReCaptchaV2Invisible(settings.SiteKey, Text, ClassName, Language, Callback, Badge),
                ReCaptchaVersion.V3 => ReCaptchaGenerator.ReCaptchaV3(settings.SiteKey, Action, Language, Callback),
                _ => ReCaptchaGenerator.ReCaptchaV2(settings.SiteKey, Size, Theme, Language, Callback, ErrorCallback, ExpiredCallback),
            };

            output.Content.AppendHtml(content);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Helper extension to render the Google ReCaptcha v2/v3.
        /// </summary>
        /// <param name="helper">Html helper object.</param>
        /// <param name="text">Text shown in on the button. Used in v2-invisible ReCaptcha.</param>
        /// <param name="className">Custom class names added to the generated button of the v2-invisible ReCaptcha.</param>
        /// <param name="size">Optional parameter, contains the size of the widget.</param>
        /// <param name="theme">Google Recaptcha theme default is light.</param>
        /// <param name="action">Google Recaptcha v3 <a href="https://developers.google.com/recaptcha/docs/v3#actions">Action</a></param>
        /// <param name="language">Google Recaptcha <a href="https://developers.google.com/recaptcha/docs/language">Language Code</a></param>
        /// <param name="id">Google ReCaptcha button id. This id can't be named 'submit' due to a naming bug. Used in v2-invisible ReCaptcha.</param>
        /// <param name="badge">Badge parameter for the v2 invisible widget. Defaults to bottomright.</param>
        /// <param name="callback">Google ReCaptcha success callback method. Used in v2 ReCaptcha.</param>
        /// <param name="errorCallback">Google ReCaptcha error callback method. Used in v2 ReCaptcha.</param>
        /// <param name="expiredCallback">Google ReCaptcha expired callback method. Used in v2 ReCaptcha.</param>
        /// <returns>HtmlString with Recaptcha elements</returns>
        public static IHtmlContent ReCaptcha(
            this IHtmlHelper helper,
            string text            = "Submit",
            string className       = "",
            string size            = "normal",
            string theme           = "light",
            string action          = "homepage",
            string language        = null,
            string id              = "recaptcha",
            string badge           = "bottomright",
            string callback        = null,
            string errorCallback   = null,
            string expiredCallback = null)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException("id can't be null");
            }

            if (id.ToLower() == "submit")
            {
                throw new ArgumentException("id can't be named submit");
            }

            language ??= helper.ViewContext.HttpContext.Features.Get <IRequestCultureFeature>()?.RequestCulture
            .UICulture.TwoLetterISOLanguageName;

            var settings = helper.ViewContext.HttpContext.RequestServices.GetRequiredService <IOptions <ReCaptchaSettings> >().Value;

            switch (settings.Version)
            {
            default:
            case ReCaptchaVersion.V2:
                return(ReCaptchaGenerator.ReCaptchaV2(settings.SiteKey, size, theme, language, callback, errorCallback, expiredCallback));

            case ReCaptchaVersion.V2Invisible:
                return(ReCaptchaGenerator.ReCaptchaV2Invisible(settings.SiteKey, text, className, language, callback, badge));

            case ReCaptchaVersion.V3:
                return(ReCaptchaGenerator.ReCaptchaV3(settings.SiteKey, action, language, callback));
            }
        }