//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)); }
public RecaptchaProps( RecaptchaType type, string siteKey, string callback = null, string expiredCallback = null, string errorCallback = null, ThemeType?theme = null, int?tabIndex = null, SizeType?size = null, BadgeType?badge = null, string action = null) { RenderProps = new RenderProps( type, siteKey, theme: theme, tabIndex: tabIndex, size: size, badge: badge, action: action); Type = type; Callback = callback.TrimToNull(); ExpiredCallback = expiredCallback.TrimToNull(); ErrorCallback = errorCallback.TrimToNull(); }
public bool ValidateRecaptcha(string response, RecaptchaType recaptchaType, string ip) { try { string privateKey; switch (recaptchaType) { case RecaptchaType.AndroidV2: privateKey = Configuration["recaptcha:private-key:android"]; break; case RecaptchaType.iOSV2: privateKey = Configuration["recaptcha:private-key:ios"]; break; default: privateKey = Configuration["recaptcha:private-key"]; break; } var data = string.Format("secret={0}&remoteip={1}&response={2}", privateKey, ip, response); var url = Configuration["recaptcha:verify-url"] ?? "https://www.recaptcha.net/recaptcha/api/siteverify"; var webRequest = (HttpWebRequest)WebRequest.Create(url); webRequest.Method = WebRequestMethods.Http.Post; webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.ContentLength = data.Length; using (var writer = new StreamWriter(webRequest.GetRequestStream())) { writer.Write(data); } using var webResponse = webRequest.GetResponse(); using var reader = new StreamReader(webResponse.GetResponseStream()); var resp = reader.ReadToEnd(); var resObj = JObject.Parse(resp); if (resObj["success"] != null && resObj.Value <bool>("success")) { return(true); } else { Log.DebugFormat("Recaptcha error: {0}", resp); } if (resObj["error-codes"] != null && resObj["error-codes"].HasValues) { Log.DebugFormat("Recaptcha api returns errors: {0}", resp); } } catch (Exception ex) { Log.Error(ex); } return(false); }
private static async Task <VerifyResponse> VerifyTokenAsync(RecaptchaType type, string responseToken, string secretKey, IPAddress remoteIp) { if (string.IsNullOrEmpty(responseToken)) { throw new ArgumentNullException(nameof(responseToken)); } var url = string.Format(VerifyUrl, secretKey, responseToken, remoteIp); var result = await new HttpClient().GetStreamAsync(url); var serializer = new DataContractJsonSerializer(typeof(VerifyResponse)); return((VerifyResponse)serializer.ReadObject(result)); }
/// <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="secretKey">The shared key between your site and reCAPTCHA.</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 async Task <VerifyResponse> VerifyAsync(string responseToken, RecaptchaType type, string secretKey = null, IPAddress remoteIp = null) { secretKey = secretKey ?? _settings?.First(type)?.SecretKey; if (string.IsNullOrEmpty(secretKey)) { throw new ArgumentNullException(nameof(secretKey)); } return(await VerifyTokenAsync( type, responseToken, secretKey, remoteIp : remoteIp)); }
public RenderProps( RecaptchaType type, string siteKey, ThemeType?theme = null, int?tabIndex = null, SizeType?size = null, BadgeType?badge = null, string action = null) { SiteKey = siteKey; Type = type; Theme = theme; TabIndex = tabIndex; Size = size; Badge = badge; Action = action; }
/// <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)); }
/// <summary> /// Returns the first <see cref="Key"/> of the sequence that matches <see cref="RecaptchaType"/> or null if no such element is found. /// </summary> public Key First(RecaptchaType type) { return(Keys?.FirstOrDefault(key => key.KeyType == type)); }
/// <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)); }