public ActionResult Captcha(FormCollection form) { var challenge = form.Get<string>("recaptcha_challenge_field", ""); var validator = new Recaptcha.RecaptchaValidator { PrivateKey = AppSettings.RecaptchaPrivateKey, RemoteIP = GetRemoteIP(), Challenge = challenge, Response = form.Get<string>("recaptcha_response_field", "") }; Recaptcha.RecaptchaResponse response; try { response = validator.Validate(); } catch (WebException) { // recaptcha is down - if the challenge had some length (it's usually a massive hash), allow the action - spam will be destroyed by the community response = challenge.Length >= 30 ? Recaptcha.RecaptchaResponse.Valid : Recaptcha.RecaptchaResponse.RecaptchaNotReachable; } if (response == Recaptcha.RecaptchaResponse.Valid) { Current.SetCachedObjectSliding(CaptchaKey(GetRemoteIP()), true, 60 * 15); return Json(new { success = true }); } return Json(new { success = false });; }
public override void OnActionExecuting(ActionExecutingContext filterContext) { bool valid = false; var captchaChallengeValue = filterContext.HttpContext.Request.Form[CHALLENGE_FIELD_KEY]; var captchaResponseValue = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY]; if (!string.IsNullOrEmpty(captchaChallengeValue) && !string.IsNullOrEmpty(captchaResponseValue)) { var captchaSettings = EngineContext.Current.Resolve<CaptchaSettings>(); if (captchaSettings.Enabled) { //validate captcha var captchaValidtor = new Recaptcha.RecaptchaValidator { PrivateKey = captchaSettings.ReCaptchaPrivateKey, RemoteIP = filterContext.HttpContext.Request.UserHostAddress, Challenge = captchaChallengeValue, Response = captchaResponseValue }; var recaptchaResponse = captchaValidtor.Validate(); valid = recaptchaResponse.IsValid; } } //this will push the result value into a parameter in our Action filterContext.ActionParameters["captchaValid"] = valid; base.OnActionExecuting(filterContext); }
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) { var jsonStr = actionContext.Request.Content.ReadAsStringAsync().Result; var msgObj = Json.Decode(jsonStr); bindingContext.Model = new Msg { UserName = msgObj.UserName, Email = msgObj.Email, HomePage = msgObj.HomePage, Text = msgObj.Text, Date = DateTime.Now }; var captchaValidtor = new Recaptcha.RecaptchaValidator { PrivateKey = ConfigurationManager.AppSettings["ReCaptchaPrivateKey"], RemoteIP = ((System.Web.HttpContextWrapper)actionContext.Request.Properties["MS_HttpContext"]).Request.UserHostAddress, Challenge = msgObj.recaptcha_challenge_field, Response = msgObj.recaptcha_response_field }; var recaptchaResponse = captchaValidtor.Validate(); if (!recaptchaResponse.IsValid) { actionContext.ModelState.AddModelError("recaptcha_response_field", "Символы не соответствуют картинке."); } return true; }
public override void OnActionExecuting(ActionExecutingContext filterContext) { bool valid = false; var captchaChallengeValue = filterContext.HttpContext.Request.Form[CHALLENGE_FIELD_KEY]; var captchaResponseValue = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY]; if (!string.IsNullOrEmpty(captchaChallengeValue) && !string.IsNullOrEmpty(captchaResponseValue)) { //validate captcha var captchaValidtor = new Recaptcha.RecaptchaValidator { //PrivateKey = "6LdIPB4TAAAAAJ3UVZFyyY9mI3DBELNFjw-ODTp7",//captchaSettings.ReCaptchaPrivateKey, PrivateKey = "6LekyyATAAAAAABSNFkB4B0gp0FE9d4JA2MtyQU7",//captchaSettings.ReCaptchaPrivateKey, RemoteIP = filterContext.HttpContext.Request.UserHostAddress, Challenge = captchaChallengeValue, Response = captchaResponseValue }; var recaptchaResponse = captchaValidtor.Validate(); valid = recaptchaResponse.IsValid; } //this will push the result value into a parameter in our Action filterContext.ActionParameters["captchaValid"] = valid; base.OnActionExecuting(filterContext); }
public ActionResult Captcha(FormCollection form) { var challenge = form.Get("recaptcha_challenge_field", ""); var validator = new Recaptcha.RecaptchaValidator { PrivateKey = AppSettings.RecaptchaPrivateKey, RemoteIP = Current.RemoteIP, Challenge = challenge, Response = form.Get("recaptcha_response_field", "") }; Recaptcha.RecaptchaResponse response; try { response = validator.Validate(); } catch (WebException) { // recaptcha is down - if the challenge had some length (it's usually a massive hash), allow the action - spam will be destroyed by the community response = challenge.Length >= 30 ? Recaptcha.RecaptchaResponse.Valid : Recaptcha.RecaptchaResponse.RecaptchaNotReachable; } if (response == Recaptcha.RecaptchaResponse.Valid) { Current.SetCachedObjectSliding(CaptchaKey(Current.RemoteIP), true, 60 * 15); return(Json(new { success = true })); } return(Json(new { success = false }));; }
public override void OnActionExecuting(ActionExecutingContext filterContext) { if (SkipRecaptcha) { filterContext.ActionParameters["captchaValid"] = true; } else { RecaptchaValidator validator = new Recaptcha.RecaptchaValidator(); validator.PrivateKey = RecaptchaControlMvc.PrivateKey; validator.RemoteIP = filterContext.HttpContext.Request.UserHostAddress; validator.Challenge = filterContext.HttpContext.Request.Form[CHALLENGE_FIELD_KEY]; validator.Response = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY]; validator.Proxy = proxy; if (string.IsNullOrEmpty(validator.Challenge)) { this.recaptchaResponse = RecaptchaResponse.InvalidChallenge; } else if (string.IsNullOrEmpty(validator.Response)) { this.recaptchaResponse = RecaptchaResponse.InvalidResponse; } else { this.recaptchaResponse = validator.Validate(); } // this will push the result values into a parameter in our Action filterContext.ActionParameters["captchaValid"] = this.recaptchaResponse.IsValid; filterContext.ActionParameters["captchaErrorMessage"] = this.recaptchaResponse.ErrorMessage; } base.OnActionExecuting(filterContext); }
public override void OnActionExecuting(ActionExecutingContext filterContext) { var privateKey = ConfigurationManager.AppSettings["recaptcha.privateKey"]; if (String.IsNullOrEmpty(privateKey)) throw new ConfigurationErrorsException("The key 'recaptcha.privateKey' must be defined in your config file"); var captchaValidator = new Recaptcha.RecaptchaValidator { PrivateKey = privateKey, RemoteIP = filterContext.HttpContext.Request.UserHostAddress, Challenge = filterContext.HttpContext.Request.Form[ChallengeFieldKey] ?? String.Empty, Response = filterContext.HttpContext.Request.Form[ResponseFieldKey] ?? String.Empty }; var captchaResponse = captchaValidator.Validate(); // Push the result value into a parameter in our Action filterContext.ActionParameters[_resultKey] = captchaResponse.IsValid; // Set the Model State error if (!captchaResponse.IsValid && _setModelStateError) { filterContext.Controller.ViewData.ModelState.AddModelError(_resultKey, _errorMessage); } base.OnActionExecuting(filterContext); }
public override void OnActionExecuting(ActionExecutingContext filterContext) { bool valid = false; var captchaChallengeValue = filterContext.HttpContext.Request.Form[CHALLENGE_FIELD_KEY]; var captchaResponseValue = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY]; if (!string.IsNullOrEmpty(captchaChallengeValue) && !string.IsNullOrEmpty(captchaResponseValue)) { var captchaSettings = EngineContext.Current.Resolve <CaptchaSettings>(); if (captchaSettings.Enabled) { //validate captcha var captchaValidtor = new Recaptcha.RecaptchaValidator { PrivateKey = captchaSettings.ReCaptchaPrivateKey, RemoteIP = filterContext.HttpContext.Request.UserHostAddress, Challenge = captchaChallengeValue, Response = captchaResponseValue }; var recaptchaResponse = captchaValidtor.Validate(); valid = recaptchaResponse.IsValid; } } //this will push the result value into a parameter in our Action filterContext.ActionParameters["captchaValid"] = valid; base.OnActionExecuting(filterContext); }
public ReturnObject CheckCaptcha(UserRequest Object) { try { OperationContext context = OperationContext.Current; MessageProperties messageProperties = context.IncomingMessageProperties; RemoteEndpointMessageProperty endpointProperty = messageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty; Recaptcha.RecaptchaValidator captchaValidtor = new Recaptcha.RecaptchaValidator { PrivateKey = System.Web.Configuration.WebConfigurationManager.AppSettings["PRIVATE_KEY"].ToString(), RemoteIP = endpointProperty.Address, Challenge = Object.challenge, Response = Object.response }; Recaptcha.RecaptchaResponse recaptchaResponse = captchaValidtor.Validate(); ReturnObject rObj = new ReturnObject(recaptchaResponse.IsValid, recaptchaResponse.ErrorMessage, ""); return rObj; } catch (Exception ex) { ReturnObject rObj = new ReturnObject(false, ex.Message, ""); return rObj; } }
public override void OnActionExecuting(ActionExecutingContext filterContext) { var privateKey = ConfigurationManager.AppSettings["RecaptchaPrivate"]; var captchaChallengeValue = filterContext.HttpContext.Request.Form[CHALLENGE_FIELD_KEY]; var captchaResponseValue = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY]; var captchaValidtor = new Recaptcha.RecaptchaValidator { PrivateKey = privateKey, RemoteIP = filterContext.HttpContext.Request.UserHostAddress, Challenge = captchaChallengeValue, Response = captchaResponseValue }; var recaptchaResponse = captchaValidtor.Validate(); // this will push the result value into a parameter in our Action filterContext.ActionParameters["CaptchaValid"] = recaptchaResponse.IsValid; if (!recaptchaResponse.IsValid && filterContext.Controller is Controller) { var controller = filterContext.Controller as Controller; controller.ModelState.AddModelError("CaptchaValid", "Captcha wasn't recognised please try again."); } base.OnActionExecuting(filterContext); }
public override void OnActionExecuting(ActionExecutingContext filterContext) { var privateKey = ConfigurationManager.AppSettings["recaptcha.privateKey"]; if (String.IsNullOrEmpty(privateKey)) { throw new ConfigurationErrorsException("The key 'recaptcha.privateKey' must be defined in your config file"); } var captchaValidator = new Recaptcha.RecaptchaValidator { PrivateKey = privateKey, RemoteIP = filterContext.HttpContext.Request.UserHostAddress, Challenge = filterContext.HttpContext.Request.Form[ChallengeFieldKey] ?? String.Empty, Response = filterContext.HttpContext.Request.Form[ResponseFieldKey] ?? String.Empty }; var captchaResponse = captchaValidator.Validate(); // Push the result value into a parameter in our Action filterContext.ActionParameters[_resultKey] = captchaResponse.IsValid; // Set the Model State error if (!captchaResponse.IsValid && _setModelStateError) { filterContext.Controller.ViewData.ModelState.AddModelError(_resultKey, _errorMessage); } base.OnActionExecuting(filterContext); }
public override void OnActionExecuting(ActionExecutingContext filterContext) { bool valid = false; var captchaChallengeValue = filterContext.HttpContext.Request.Form[CHALLENGE_FIELD_KEY]; var captchaResponseValue = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY]; if (!string.IsNullOrEmpty(captchaChallengeValue) && !string.IsNullOrEmpty(captchaResponseValue)) { //validate captcha var captchaValidtor = new Recaptcha.RecaptchaValidator { //PrivateKey = "6LdIPB4TAAAAAJ3UVZFyyY9mI3DBELNFjw-ODTp7",//captchaSettings.ReCaptchaPrivateKey, PrivateKey = "6LekyyATAAAAAABSNFkB4B0gp0FE9d4JA2MtyQU7", //captchaSettings.ReCaptchaPrivateKey, RemoteIP = filterContext.HttpContext.Request.UserHostAddress, Challenge = captchaChallengeValue, Response = captchaResponseValue }; var recaptchaResponse = captchaValidtor.Validate(); valid = recaptchaResponse.IsValid; } //this will push the result value into a parameter in our Action filterContext.ActionParameters["captchaValid"] = valid; base.OnActionExecuting(filterContext); }
public override void OnActionExecuting(ActionExecutingContext filterContext) { var privateKey = ConfigurationManager.AppSettings["RecaptchaPrivate"]; var captchaChallengeValue = filterContext.HttpContext.Request.Form[CHALLENGE_FIELD_KEY]; var captchaResponseValue = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY]; var captchaValidtor = new Recaptcha.RecaptchaValidator { PrivateKey = privateKey, RemoteIP = filterContext.HttpContext.Request.UserHostAddress, Challenge = captchaChallengeValue, Response = captchaResponseValue }; var recaptchaResponse = captchaValidtor.Validate(); // this will push the result value into a parameter in our Action filterContext.ActionParameters["CaptchaValid"] = recaptchaResponse.IsValid; if (!recaptchaResponse.IsValid && filterContext.Controller is Controller) { var controller = filterContext.Controller as Controller; controller.ModelState.AddModelError("CaptchaValid", "Captcha wasn't recognised please try again."); } base.OnActionExecuting(filterContext); }
/// <summary> /// validate a captcha challenge /// </summary> /// <param name="captchaChallengeValue"></param> /// <param name="captchaResponseValue"></param> /// <returns></returns> public static bool Validate(string privatekey, string captchaChallengeValue, string captchaResponseValue) { var captchaValidtor = new Recaptcha.RecaptchaValidator { PrivateKey = privatekey, RemoteIP = HttpContext.Current.Request.UserHostAddress, Challenge = captchaChallengeValue, Response = captchaResponseValue }; var recaptchaResponse = captchaValidtor.Validate(); return recaptchaResponse.IsValid; }
public bool myValidator(string captchaChallengeValue, string captchaResponseValue) { var captchaValidtor = new Recaptcha.RecaptchaValidator { PrivateKey = ConfigurationManager.AppSettings["ReCaptchaPrivateKey"], RemoteIP = HttpContext.Current.Request.UserHostAddress, // Request.UserHostAddress, Challenge = captchaChallengeValue, Response = captchaResponseValue }; var recaptchaResponse = captchaValidtor.Validate(); return recaptchaResponse.IsValid; }
public bool IsCaptchaValid(string captchaChallengeValue, string captchaResponseValue, string userHostAddress) { _captchaSettings = _orchardServices.WorkContext.CurrentSite.As <CaptchaSettingsPart>(); var captchaValidtor = new Recaptcha.RecaptchaValidator { PrivateKey = _captchaSettings.PrivateKey, RemoteIP = userHostAddress, Challenge = captchaChallengeValue, Response = captchaResponseValue }; var recaptchaResponse = captchaValidtor.Validate(); // this will push the result value into a parameter in our Action return(recaptchaResponse.IsValid); }
public override void OnActionExecuting(ActionExecutingContext filterContext) { var captchaChallengeValue = filterContext.HttpContext.Request.Form[ChallengeFieldKey]; var captchaResponseValue = filterContext.HttpContext.Request.Form[ResponseFieldKey]; var captchaValidtor = new Recaptcha.RecaptchaValidator { PrivateKey = ConfigurationManager.AppSettings["ReCaptchaPrivateKey"], RemoteIP = filterContext.HttpContext.Request.UserHostAddress, Challenge = captchaChallengeValue, Response = captchaResponseValue }; var recaptchaResponse = captchaValidtor.Validate(); filterContext.ActionParameters["captchaValid"] = recaptchaResponse.IsValid; base.OnActionExecuting(filterContext); }
public bool IsCaptchaValid(FormCollection form, string userHostAddress) { var captchaChallengeValue = form[ChallengeFieldKey]; var captchaResponseValue = form[ResponseFieldKey]; var captchaValidtor = new Recaptcha.RecaptchaValidator { PrivateKey = CaptchaPart.PrivateKey, RemoteIP = userHostAddress, Challenge = captchaChallengeValue, Response = captchaResponseValue }; var recaptchaResponse = captchaValidtor.Validate(); // this will push the result value into a parameter in our Action return recaptchaResponse.IsValid; }
public override void OnActionExecuting(ActionExecutingContext filterContext) { var captChallengeValue = filterContext.HttpContext.Request.Form[CHALLENGE_FIELD_KEY]; var captResponseValue = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY]; var catpchaValidator = new Recaptcha.RecaptchaValidator { PrivateKey = MvcApplication.CaptchaPrivateKey, RemoteIP = filterContext.HttpContext.Request.UserHostAddress, Challenge = captChallengeValue, Response = captResponseValue }; var recaptchaResponse = catpchaValidator.Validate(); filterContext.ActionParameters["validCaptcha"] = recaptchaResponse.IsValid; base.OnActionExecuting(filterContext); }
public override void OnActionExecuting(ActionExecutingContext filterContext) { var captchaChallengeValue = filterContext.HttpContext.Request.Form[CHALLENGE_FIELD_KEY]; var captchaResponseValue = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY]; var captchaValidtor = new Recaptcha.RecaptchaValidator { PrivateKey = "6LeAousSAAAAADqiHv12jipV3AsBUrJ9DJuUaQId", RemoteIP = filterContext.HttpContext.Request.UserHostAddress, Challenge = captchaChallengeValue, Response = captchaResponseValue }; var recaptchaResponse = captchaValidtor.Validate(); // this will push the result value into a parameter in our Action filterContext.ActionParameters["captchaValid"] = recaptchaResponse.IsValid; base.OnActionExecuting(filterContext); }
public override void OnActionExecuting(ActionExecutingContext filterContext) { var captchaChallengeValue = filterContext.HttpContext.Request.Form[CHALLENGE_FIELD_KEY]; var captchaResponseValue = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY]; var captchaValidtor = new Recaptcha.RecaptchaValidator { PrivateKey = ConfigurationManager.AppSettings["RecaptchaPrivateKey"], RemoteIP = filterContext.HttpContext.Request.UserHostAddress, Challenge = captchaChallengeValue, Response = captchaResponseValue }; var recaptchaResponse = captchaValidtor.Validate(); // this will push the result value into a parameter in our Action filterContext.ActionParameters["captchaValid"] = recaptchaResponse.IsValid; base.OnActionExecuting(filterContext); }
public override void OnActionExecuting(ActionExecutingContext filterContext) { var captchaChallengeValue = filterContext.HttpContext.Request.Form[CHALLENGE_FIELD_KEY]; var captchaResponseValue = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY]; var captchaValidtor = new Recaptcha.RecaptchaValidator { PrivateKey = "6Lc-KwYAAAAAANt8fE-8H3KtrFClNIz4f5FSqG4p", RemoteIP = filterContext.HttpContext.Request.UserHostAddress, Challenge = captchaChallengeValue, Response = captchaResponseValue }; var recaptchaResponse = captchaValidtor.Validate(); // this will push the result value into a parameter in our Action filterContext.ActionParameters["captchaValid"] = recaptchaResponse.IsValid; base.OnActionExecuting(filterContext); }
public override void OnActionExecuting(ActionExecutingContext filterContext) { var captchaChallengeValue = filterContext.HttpContext.Request.Form[CHALLENGE_FIELD_KEY]; var captchaResponseValue = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY]; var captchaValidator = new Recaptcha.RecaptchaValidator { PrivateKey = ConfigurationManager.AppSettings["reCaptcha-PrivateKey"], RemoteIP = filterContext.HttpContext.Request.UserHostAddress, Challenge = captchaChallengeValue, Response = captchaResponseValue }; var recaptchaResponse = captchaValidator.Validate(); // this will push the result value into a parameter in our Action filterContext.ActionParameters["captchaValid"] = recaptchaResponse.IsValid; base.OnActionExecuting(filterContext); }
/// <summary> /// Called before the action method is invoked /// </summary> /// <param name="filterContext">Information about the current request and action</param> public override void OnActionExecuting(ActionExecutingContext filterContext) { var captchaChallengeValue = filterContext.HttpContext.Request.Form[ChallengeFieldKey]; var captchaResponseValue = filterContext.HttpContext.Request.Form[ResponseFieldKey]; var captchaValidtor = new Recaptcha.RecaptchaValidator { PrivateKey = ConfigurationManager.AppSettings["ReCaptchaPrivateKey"], RemoteIP = filterContext.HttpContext.Request.UserHostAddress, Challenge = captchaChallengeValue, Response = captchaResponseValue }; var recaptchaResponse = captchaValidtor.Validate(); // this will push the result value into a parameter in our Action filterContext.ActionParameters["captchaValid"] = recaptchaResponse.IsValid; if (!recaptchaResponse.IsValid) filterContext.Controller.ViewData.ModelState.AddModelError("recaptcha", "incorrect user input"); base.OnActionExecuting(filterContext); }
public override void OnActionExecuting(ActionExecutingContext filterContext) { var captchaChallengeValue = filterContext.HttpContext.Request.Form[CHALLENGE_FIELD_KEY]; var captchaResponseValue = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY]; var captchaValidtor = new Recaptcha.RecaptchaValidator { PrivateKey = "key", RemoteIP = filterContext.HttpContext.Request.UserHostAddress, Challenge = captchaChallengeValue, Response = captchaResponseValue }; var recaptchaResponse = captchaValidtor.Validate(); if (!recaptchaResponse.IsValid) { filterContext.Controller .ViewData.ModelState .AddModelError( CAPTCHA_MODEL_KEY, "Entered text is invalid"); } base.OnActionExecuting(filterContext); }
/// <summary> /// Called before the action method is invoked /// </summary> /// <param name="filterContext">Information about the current request and action</param> public override void OnActionExecuting(ActionExecutingContext filterContext) { var captchaChallengeValue = filterContext.HttpContext.Request.Form[ChallengeFieldKey]; var captchaResponseValue = filterContext.HttpContext.Request.Form[ResponseFieldKey]; var captchaValidtor = new Recaptcha.RecaptchaValidator { PrivateKey = ConfigurationManager.AppSettings["ReCaptchaPrivateKey"], RemoteIP = filterContext.HttpContext.Request.UserHostAddress, Challenge = captchaChallengeValue, Response = captchaResponseValue }; var recaptchaResponse = captchaValidtor.Validate(); // this will push the result value into a parameter in our Action filterContext.ActionParameters["captchaValid"] = recaptchaResponse.IsValid; base.OnActionExecuting(filterContext); // Add string to Trace for testing //filterContext.HttpContext.Trace.Write("Log: OnActionExecuting", String.Format("Calling {0}", filterContext.ActionDescriptor.ActionName)); }
/// <summary> /// Verify that the given form contains a solution to a captcha. /// /// If not, message will contain something suitable for display to a user. /// </summary> public static bool Verify(NameValueCollection form, out string message) { var challenge = form["recaptcha_challenge_field"]; var response = form["recaptcha_response_field"]; if (!challenge.HasValue() || !response.HasValue()) { message = "Captcha failed"; return(false); } var captcha = new Recaptcha.RecaptchaValidator(); captcha.RemoteIP = Current.RemoteIP; captcha.Challenge = challenge; captcha.Response = response; captcha.PrivateKey = WebConfigurationManager.AppSettings["ReCaptchaPrivateKey"]; try { var res = captcha.Validate(); if (res != Recaptcha.RecaptchaResponse.Valid) { message = "Captcha failed - " + res.ErrorMessage; return(false); } } catch (WebException) { message = "There was a problem communicating with ReCaptcha, please try again"; return(false); } message = ""; return(true); }
/// <summary> /// Called before the action method is invoked /// </summary> /// <param name="filterContext">Information about the current request and action</param> public override void OnActionExecuting(ActionExecutingContext filterContext) { var captchaChallengeValue = filterContext.HttpContext.Request.Form[ChallengeFieldKey]; var captchaResponseValue = filterContext.HttpContext.Request.Form[ResponseFieldKey]; var captchaValidtor = new Recaptcha.RecaptchaValidator { PrivateKey = ConfigurationManager.AppSettings["ReCaptchaPrivateKey"], RemoteIP = filterContext.HttpContext.Request.UserHostAddress, Challenge = captchaChallengeValue, Response = captchaResponseValue }; var recaptchaResponse = captchaValidtor.Validate(); // this will push the result value into a parameter in our Action filterContext.ActionParameters["captchaValid"] = recaptchaResponse.IsValid; base.OnActionExecuting(filterContext); // Add string to Trace for testing //filterContext.HttpContext.Trace.Write("Log: OnActionExecuting", String.Format("Calling {0}", filterContext.ActionDescriptor.ActionName)); }
/// <summary> /// Verify that the given form contains a solution to a captcha. /// /// If not, message will contain something suitable for display to a user. /// </summary> public static bool Verify(NameValueCollection form, out string message) { var challenge = form["recaptcha_challenge_field"]; var response = form["recaptcha_response_field"]; if (!challenge.HasValue() || !response.HasValue()) { message = "Captcha failed"; return false; } var captcha = new Recaptcha.RecaptchaValidator(); captcha.RemoteIP = Current.RemoteIP; captcha.Challenge = challenge; captcha.Response = response; captcha.PrivateKey = WebConfigurationManager.AppSettings["ReCaptchaPrivateKey"]; try { var res = captcha.Validate(); if (res != Recaptcha.RecaptchaResponse.Valid) { message = "Captcha failed - " + res.ErrorMessage; return false; } } catch (WebException) { message = "There was a problem communicating with ReCaptcha, please try again"; return false; } message = ""; return true; }
protected void LoginUser_OnAuthenticate(object sender, AuthenticateEventArgs e) { // always set to false e.Authenticated = false; try { var validator = new Recaptcha.RecaptchaValidator { PrivateKey = RECAPTCHA_SECRET_KEY, RemoteIP = Utils.GetClientIP(), Response = Context.Request.Form["g-recaptcha-response"] }; var result = validator.Validate(); if (result.IsValid) { Security.AuthenticateUser(LoginUser.UserName, LoginUser.Password, LoginUser.RememberMeSet); } } catch (ThreadAbortException) { } catch (ArgumentNullException) { } catch (Exception ex) { Utils.Log("Login User Authenticate", ex); } }
public override void OnActionExecuting(ActionExecutingContext filterContext) { if (SkipRecaptcha) { filterContext.ActionParameters["captchaValid"] = true; } else { RecaptchaValidator validator = new Recaptcha.RecaptchaValidator(); validator.PrivateKey = RecaptchaControlMvc.PrivateKey; validator.RemoteIP = filterContext.HttpContext.Request.UserHostAddress; validator.Challenge = filterContext.HttpContext.Request.Form[CHALLENGE_FIELD_KEY]; validator.Response = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY]; validator.Proxy = proxy; if (string.IsNullOrEmpty(validator.Challenge)) { this.recaptchaResponse = RecaptchaResponse.InvalidChallenge; } else if (string.IsNullOrEmpty(validator.Response)) { this.recaptchaResponse = RecaptchaResponse.InvalidResponse; } else { this.recaptchaResponse = validator.Validate(); } // this will push the result values into a parameter in our Action filterContext.ActionParameters["captchaValid"] = this.recaptchaResponse.IsValid; filterContext.ActionParameters["captchaErrorMessage"] = this.recaptchaResponse.ErrorMessage; } base.OnActionExecuting(filterContext); }
private bool IsValidCustom(string challengeValue, string responseValue) { if (string.IsNullOrWhiteSpace(challengeValue) || string.IsNullOrWhiteSpace(responseValue)) return false; Recaptcha.RecaptchaValidator captchaValidtor = new Recaptcha.RecaptchaValidator { PrivateKey = Convert.ToString("6LdoxNQSAAAAAJdWATlAmxo6S7apKJ9XpqWMoI0u"), // Get Private key of the CAPTCHA from Web.config file. RemoteIP = HttpContext.Current.Request.UserHostAddress, Challenge = challengeValue, Response = responseValue }; Recaptcha.RecaptchaResponse recaptchaResponse = captchaValidtor.Validate(); // Send data about captcha validation to reCAPTCHA site. return recaptchaResponse.IsValid; }
public bool captchaValidate() { string ChallengeFieldKey = "recaptcha_challenge_field"; string ResponseFieldKey = "recaptcha_response_field"; var captchaChallengeValue = HttpContext.Request.Form[ChallengeFieldKey]; var captchaResponseValue = HttpContext.Request.Form[ResponseFieldKey]; var captchaValidtor = new Recaptcha.RecaptchaValidator { PrivateKey = ConfigurationManager.AppSettings["ReCaptchaPrivateKey"], RemoteIP = HttpContext.Request.UserHostAddress, Challenge = captchaChallengeValue, Response = captchaResponseValue }; var recaptchaResponse = captchaValidtor.Validate(); return recaptchaResponse.IsValid; }