//private const string DefaultAction = "DefaultAction"; /// <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="settings">reCAPTCHA settings from configuration that contain a 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, RecaptchaSettings settings, 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 (settings is null) { throw new ArgumentNullException(nameof(settings)); } return(Recaptcha( htmlHelper, expression, type, settings.First(type)?.SiteKey, theme, size, tabIndex, callback, expiredCallback, errorCallback, action, badge)); }
/// <summary> /// Verifies a user's response to a reCAPTCHA challenge in an asynchronous operation. /// </summary> /// <param name="responseToken">The user response token provided by the reCAPTCHA client-side integration on your site.</param> /// <param name="type">The type of reCAPTCHA being verified.</param> /// <param name="settings"><see cref="RecaptchaSettings"/> object that has a secret key of the <see cref="RecaptchaType"/> specificed in the type parameter.</param> /// <param name="remoteIp">The user's IP address.</param> /// <returns><see cref="Task{VerifyResponse}"/> object containing the response from reCAPTCHA verification service.</returns> public static async Task <VerifyResponse> VerifyTokenAsync(string responseToken, RecaptchaType type, RecaptchaSettings settings, IPAddress remoteIp = null) { if (string.IsNullOrEmpty(settings?.First(type)?.SecretKey)) { throw new ArgumentNullException("SecretKey", "The secret key of the required type, is not found or null in RecaptchaSettings argument."); } return(await VerifyTokenAsync( type, responseToken, settings.First(type).SecretKey, remoteIp : remoteIp)); }
/// <summary> /// Constructor used when adding service to apps <see cref="IServiceCollection"/> /// </summary> /// <param name="settings"><see cref="RecaptchaSettings"/> object that has reCAPTCHA keys from configuration.</param> public RecaptchaService(IOptions <RecaptchaSettings> settings) { _settings = settings.Value; }