Ejemplo n.º 1
0
 /// <summary>
 /// 执行Action时
 /// </summary>
 /// <param name="filterContext"></param>
 public override void OnActionExecuting(ActionExecutingContext filterContext)
 {
     if (CaptchaUtility.UseCaptcha(scenarios, true))
     {
         try
         {
             ICaptchaManager captchaManager = DIContainer.Resolve <ICaptchaManager>();
             ControllerBase  controllerBase = filterContext.Controller;
             if (!captchaManager.IsCaptchaValid(filterContext))
             {
                 controllerBase.ViewData.ModelState.AddModelError("Captcha", CaptchaVerifyError);
             }
             else if (controllerBase.ViewData.ModelState.IsValid)
             {
                 //表单通过验证时,重设计数
                 CaptchaUtility.ResetLimitTryCount(scenarios);
             }
         }
         catch
         {
             throw new ExceptionFacade("检查验证码时,出现异常");
         }
     }
     base.OnActionExecuting(filterContext);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 输出验证码
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="scenarios">使用场景</param>
        /// <param name="showCaptchaImage">默认是否显示验证码图片(仅针对验证码图片可以不立即显示的情况)</param>
        /// <param name="templateName">模板名称</param>
        /// <returns></returns>
        public static MvcHtmlString Captcha <TModel>(this HtmlHelper <TModel> htmlHelper, VerifyScenarios scenarios = VerifyScenarios.Post, bool showCaptchaImage = false, string templateName = "Captcha")
        {
            if (!CaptchaUtility.UseCaptcha(scenarios))
            {
                return(MvcHtmlString.Empty);
            }
            ICaptchaManager captchaManager = DIContainer.Resolve <ICaptchaManager>();
            MvcHtmlString   captchaText    = captchaManager.GenerateCaptcha(htmlHelper, showCaptchaImage);

            return(htmlHelper.EditorForModel(templateName, new { CaptchaText = captchaText }));
        }