Ejemplo n.º 1
0
        /// <summary>
        /// Process
        /// </summary>
        /// <param name="context">Context</param>
        /// <param name="output">Output</param>
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            //contextualize IHtmlHelper
            var viewContextAware = _htmlHelper as IViewContextAware;

            viewContextAware?.Contextualize(ViewContext);

            //generate captcha control
            var captchaControl = new GRecaptchaControl(_captchaSettings.ReCaptchaVersion)
            {
                Theme     = _captchaSettings.ReCaptchaTheme,
                Id        = "recaptcha",
                PublicKey = _captchaSettings.ReCaptchaPublicKey,
                Language  = _captchaSettings.ReCaptchaLanguage
            };
            var captchaControlHtml = captchaControl.RenderControl();

            //tag details
            output.TagName = "div";
            output.TagMode = TagMode.StartTagAndEndTag;
            output.Content.SetHtmlContent(captchaControlHtml);
        }
Ejemplo n.º 2
0
        public static string GenerateCaptcha(this HtmlHelper helper)
        {
            var captchaSettings = EngineContext.Current.Resolve <CaptchaSettings>();
            var htmlWriter      = new HtmlTextWriter(new StringWriter());

            var captchaControl = new GRecaptchaControl(captchaSettings.ReCaptchaVersion)
            {
                Theme     = captchaSettings.ReCaptchaTheme,
                Id        = "recaptcha",
                PublicKey = captchaSettings.ReCaptchaPublicKey,
                Language  = captchaSettings.ReCaptchaLanguage
            };

            captchaControl.RenderControl(htmlWriter);

            return(htmlWriter.InnerWriter.ToString());
        }
Ejemplo n.º 3
0
        //ADDED for blazor
        /// <summary>
        /// Generate reCAPTCHA Control <see cref="Nop.Web.Framework.Security.Captcha.HtmlExtensions"/>
        /// </summary>
        /// <param name="helper">HTML helper</param>
        /// <returns>Result</returns>
        public static CapthaConfig GenerateScriptSourceCaptcha(this IHtmlHelper helper)
        {
            var captchaSettings = EngineContext.Current.Resolve <CaptchaSettings>();
            var workContext     = EngineContext.Current.Resolve <IWorkContext>();

            var lang = captchaSettings.ReCaptchaDefaultLanguage;

            if (captchaSettings.AutomaticallyChooseLanguage)
            {
                //this list got from this site: https://developers.google.com/recaptcha/docs/language, but we use languages only with two letters in the code
                var supportedLanguageCodes = new List <string> {
                    "af", "am", "ar", "az", "bg", "bn", "ca", "cs", "da", "de", "el", "en", "es", "et", "eu", "fa", "fi", "fil", "fr", "gl", "gu", "hi", "hr", "hu", "hy", "id", "is", "it", "iw", "ja", "ka", "kn", "ko", "lo", "lt", "lv", "ml", "mn", "mr", "ms", "nl", "no", "pl", "pt", "ro", "ru", "si", "sk", "sl", "sr", "sv", "sw", "ta", "te", "th", "tr", "uk", "ur", "vi", "zu"
                };

                var languageService  = EngineContext.Current.Resolve <ILanguageService>();
                var twoLetterIsoCode = workContext.WorkingLanguage != null
                    ? languageService.GetTwoLetterIsoLanguageName(workContext.WorkingLanguage).ToLower()
                    : string.Empty;

                lang = supportedLanguageCodes.Contains(twoLetterIsoCode) ? twoLetterIsoCode : lang;
            }

            //generate captcha control
            var captchaControl = new GRecaptchaControl
            {
                Theme     = captchaSettings.ReCaptchaTheme,
                Id        = "recaptcha",
                PublicKey = captchaSettings.ReCaptchaPublicKey,
                Language  = lang
            };

            captchaControl.RenderControl();

            return(new CapthaConfig
            {
                Id = captchaControl.Id,
                SiteKey = captchaControl.PublicKey,
                Theme = captchaControl.Theme,
                ScriptSource = captchaControl.ScriptSource
            });
        }