Beispiel #1
0
        /// <summary>
        /// Helper extension to render the Google Recaptcha.
        /// </summary>
        /// <param name="helper">Html helper object.</param>
        /// <param name="settings">Recaptcha settings needed to render.</param>
        /// <param name="action">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>
        /// <returns>HtmlString with Recaptcha elements</returns>
        public static HtmlString Recaptcha(this IHtmlHelper helper, RecaptchaSettings settings, string theme = "light", string action = "homepage")
        {
            var uid    = Guid.NewGuid().ToString();
            var method = uid.Replace("-", "_");

            switch (settings.RecaptchaVersion)
            {
            default:
            case "v2":
                return(new HtmlString(
                           $"<div id=\"{uid}\" class=\"g-recaptcha\" data-sitekey=\"{settings.RecaptchaSiteKey}\"></div>\r\n" +
                           "<script>\r\n" +
                           $"function _{method}() {{\r\n if (typeof grecaptcha !== 'undefined') {{\r\ngrecaptcha.render(\'{uid}\', {{\'sitekey\' : \'{settings.RecaptchaSiteKey}\', 'theme\' : \'{theme}\' }});}}\r\n}}\r\n" +
                           "</script>" +
                           $"<script src=\"https://www.google.com/recaptcha/api.js?onload=_{method}&render=explicit\" async defer></script>\r\n"));

            case "v3":
                return(new HtmlString(
                           $"<input id=\"g-recaptcha-response\" name=\"g-recaptcha-response\" type=\"hidden\" value=\"\" />\r\n" +
                           $"<script src=\"https://www.google.com/recaptcha/api.js?render={settings.RecaptchaSiteKey}\"></script>\r\n" +
                           "<script>\r\n" +
                           $"if (typeof grecaptcha !== \'undefined\') {{\r\n grecaptcha.ready(function () {{\r\n grecaptcha.execute(\'{settings.RecaptchaSiteKey}\', {{ \'action\': \'{action}\' }}).then(function (token) {{\r\n document.getElementById(\'g-recaptcha-response\').value = token;\r\n }})\r\n }})\r\n}}" +
                           "</script>"));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Helper extension to render the Google Recaptcha.
        /// </summary>
        /// <param name="helper">Html helper object.</param>
        /// <param name="settings">Recaptcha settings needed to render.</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 v2-invis button id. This id can't be named submit due to a naming bug.</param>
        /// <param name="successCallback">Google Recaptcha v2/v2-invis success callback method.</param>
        /// <param name="errorCallback">Google Recaptcha v2/v2-invis error callback method.</param>
        /// <param name="expiredCallback">Google Recaptcha v2/v2-invis expired callback method.</param>
        /// <returns>HtmlString with Recaptcha elements</returns>
        public static HtmlString Recaptcha(this IHtmlHelper helper, RecaptchaSettings settings, string theme = "light", string action = "homepage", string language = "en", string id = "recaptcha", string successCallback = null, string errorCallback = null, string expiredCallback = null)
        {
            if (!settings.Enabled)
            {
                return(new HtmlString("<!-- Google Recaptcha disabled -->"));
            }

            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");
            }

            var uid    = Guid.NewGuid();
            var method = uid.ToString().Replace("-", "_");

            switch (settings.Version)
            {
            default:
            case "v2":
                return(new HtmlString(new v2(new v2Model()
                {
                    Settings = settings,
                    Uid = uid,
                    Method = method,
                    Theme = theme,
                    Language = language,
                    SuccessCallback = successCallback,
                    ErrorCallback = errorCallback,
                    ExpiredCallback = expiredCallback
                }).TransformText()));

            case "v2-invis":
                return(new HtmlString(new v2Invis(new v2Model()
                {
                    Settings = settings,
                    Id = id,
                    Uid = uid,
                    Method = method,
                    Theme = theme,
                    Language = language,
                    SuccessCallback = successCallback,
                    ErrorCallback = errorCallback,
                    ExpiredCallback = expiredCallback
                }).TransformText()));

            case "v3":
                return(new HtmlString(new v3(new v3Model()
                {
                    Settings = settings,
                    Uid = uid,
                    Action = action,
                    Language = language
                }).TransformText()));
            }
        }
Beispiel #3
0
        /// <summary>
        /// Helper extension to render the Google Recaptcha.
        /// </summary>
        /// <param name="helper">Html helper object.</param>
        /// <param name="settings">Recaptcha settings needed to render.</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>
        /// <returns>HtmlString with Recaptcha elements</returns>
        public static HtmlString Recaptcha(this IHtmlHelper helper, RecaptchaSettings settings, string theme = "light", string action = "homepage")
        {
            var uid    = Guid.NewGuid();
            var method = uid.ToString().Replace("-", "_");

            switch (settings.Version)
            {
            default:
            case "v2":
                return(new HtmlString(new v2(new v2Model()
                {
                    Settings = settings,
                    Uid = uid,
                    Method = method,
                    Theme = theme
                }).TransformText()));

            case "v2-invis":
                return(new HtmlString(new v2Invis(new v2Model()
                {
                    Settings = settings,
                    Uid = uid,
                    Method = method,
                    Theme = theme
                }).TransformText()));

            case "v3":
                return(new HtmlString(new v3(new v3Model()
                {
                    Settings = settings,
                    Uid = uid,
                    Action = action
                }).TransformText()));
            }
        }
        /// <summary>
        /// Helper extension to render the Google Recaptcha.
        /// </summary>
        /// <param name="helper">Html helper object.</param>
        /// <param name="settings">Recaptcha settings needed to render.</param>
        /// <param name="model">Optional recaptcha version model. If not supplied a model with defaults will be created.</param>
        /// <returns>HtmlString with Recaptcha elements.</returns>
        public static HtmlString Recaptcha <T>(this IHtmlHelper helper, RecaptchaSettings settings, T model = default(T))
        {
            if (settings == null)
            {
                throw new ArgumentException("settings can't be null");
            }

            var    name = typeof(T)?.Name;
            object instance;

            if (model == null)
            {
                instance = name switch
                {
                    nameof(RecaptchaV2Checkbox) => new RecaptchaV2Checkbox {
                        Settings = settings
                    },
                    nameof(RecaptchaV2Invisible) => new RecaptchaV2Invisible {
                        Settings = settings
                    },
                    nameof(RecaptchaV3HiddenInput) => new RecaptchaV3HiddenInput {
                        Settings = settings
                    },
                    _ => throw new ArgumentException(
                              $"Unknown type '{name}' passed as recaptcha version. Please use a valid type for T when using the Recaptcha method.")
                };
            }
            else
            {
                instance = Convert.ChangeType(model, typeof(T));
            }

            string body;

            switch (name)
            {
            case nameof(RecaptchaV2Checkbox):
                var v2Checkbox = (RecaptchaV2Checkbox)instance;
                body = new Templates.RecaptchaV2Checkbox(v2Checkbox).TransformText();
                break;

            case nameof(RecaptchaV2Invisible):
                var v2Invisible = (RecaptchaV2Invisible)instance;
                body = new Templates.RecaptchaV2Invisible(v2Invisible).TransformText();
                break;

            case nameof(RecaptchaV3HiddenInput):
                var v3 = (RecaptchaV3HiddenInput)instance;
                body = new Templates.RecaptchaV3HiddenInput(v3).TransformText();
                break;

            default:
                body = string.Empty;
                break;
            }

            return(new HtmlString(body));
        }
Beispiel #5
0
        public RecaptchaService(IOptions <RecaptchaSettings> options)
        {
            RecaptchaSettings = options.Value;

            if (Client == null)
            {
                Client = new HttpClient();
            }
        }
Beispiel #6
0
        public RecaptchaService(RecaptchaSettings options)
        {
            RecaptchaSettings = options;

            if (Client == null)
            {
                Client = new HttpClient();
            }
        }
Beispiel #7
0
        /// <summary>
        /// Adds services required for using reCAPTCHA.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
        /// <param name="configurationSection">The configuration section that contains reCAPTCHA settings.</param>
        /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
        public static IServiceCollection AddRecaptcha(this IServiceCollection services, IConfiguration configurationSection)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

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

            var recaptchaSettings = new RecaptchaSettings();

            configurationSection.Bind(recaptchaSettings);
            services.AddSingleton(recaptchaSettings);
            services.AddTransient <IRecaptchaService, RecaptchaService>();

            return(services);
        }
        /// <summary>
        /// Adds services required for using reCAPTCHA.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
        /// <param name="options">The action used to configure reCAPTCHA.</param>
        /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
        public static IServiceCollection AddRecaptcha(this IServiceCollection services, Action <RecaptchaSettings> options)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

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

            var recaptchaSettings = new RecaptchaSettings();

            options.Invoke(recaptchaSettings);

            services.Configure(options);
            services.AddTransient <IRecaptchaService, RecaptchaService>();

            return(services);
        }
Beispiel #9
0
 public RecaptchaService(RecaptchaSettings settings, HttpClient client)
 {
     recaptchaSettings = settings;
     Client            = client;
 }
Beispiel #10
0
 public RecaptchaService(RecaptchaSettings settings)
 {
     recaptchaSettings = settings;
     Client            = new HttpClient();
 }
Beispiel #11
0
 public RecaptchaService(RecaptchaSettings options, HttpClient client)
 {
     RecaptchaSettings = options;
     Client            = client;
 }
Beispiel #12
0
 public RecaptchaService(IOptions <RecaptchaSettings> options, HttpClient client)
 {
     RecaptchaSettings = options.Value;
     Client            = client;
 }
Beispiel #13
0
 public RecaptchaService(IOptions <RecaptchaSettings> options)
 {
     RecaptchaSettings = options.Value;
 }
 public RecaptchaService(IOptions <RecaptchaSettings> options)
 {
     RecaptchaSettings = options.Value;
     Client ??= new HttpClient();
 }