/// <summary> /// Check for proper input captcha /// </summary> /// <param name="controllerBase"></param> /// <param name="textError">text for error</param> /// <returns></returns> public static bool IsCaptchaVerify(this ControllerBase controllerBase, string textError) { try { var captchaModel = new CaptchaModel { CaptchaDeText = controllerBase.ValueProvider.GetValue("CaptchaDeText").AttemptedValue, CaptchaInputText = controllerBase.ValueProvider.GetValue("CaptchaInputText").AttemptedValue }; controllerBase.ViewData.ModelState.Remove("CaptchaDeText"); controllerBase.ViewData.ModelState.Remove("CaptchaInputText"); var isVerify = IsVerify(captchaModel); if (!isVerify) controllerBase.ViewData.ModelState.AddModelError("CaptchaInputText", textError); return isVerify; } catch (Exception) { throw new NullReferenceException("Form not contain CaptchaModel"); } }
public override void OnActionExecuting(ActionExecutingContext filterContext) { try { ControllerBase controllerBase = filterContext.Controller; var captchaModel = new CaptchaModel { CaptchaDeText = controllerBase.ValueProvider.GetValue("CaptchaDeText").AttemptedValue, CaptchaInputText = controllerBase.ValueProvider.GetValue("CaptchaInputText").AttemptedValue }; if (!CaptchaHelper.IsVerify(captchaModel)) controllerBase.ViewData.ModelState.AddModelError("CaptchaInputText", _textError); } catch (Exception) { throw new NullReferenceException("Form not contain CaptchaModel"); } base.OnActionExecuting(filterContext); }
public static bool Verify(CaptchaModel model) { return IsVerify(model); }
/// <summary> /// Help method for verify /// </summary> /// <param name="captcha"></param> /// <returns></returns> internal static bool IsVerify(CaptchaModel captcha) { try { var encryptorModel = GetEncryptorModel(); if (encryptorModel == null) return false; var textDecrypt = GetEncryption().Decrypt(captcha.CaptchaDeText, encryptorModel.Password, encryptorModel.Salt); return textDecrypt == captcha.CaptchaInputText; } catch { return false; } }