/// <summary> /// Called when [action executed]. /// </summary> /// <param name="filterContext">The filter filterContext.</param> public override void OnActionExecuting(ActionExecutingContext filterContext) { bool isValid = false; // get the guid from the post back string guid = filterContext.HttpContext.Request.Form["_MvcCaptchaGuid"]; var captchaSettings = EngineContext.Current.Resolve <CaptchaSettings>(); if (captchaSettings.Enabled) { // get values var image = MvcCaptchaImage.GetCachedCaptcha(guid); string actualValue = filterContext.HttpContext.Request.Form[Field]; string expectedValue = image == null ? String.Empty : image.Text; // removes the captch from Session so it cannot be used again filterContext.HttpContext.Session.Remove(guid); isValid = !String.IsNullOrEmpty(actualValue) && !String.IsNullOrEmpty(expectedValue) && String.Equals(actualValue, expectedValue, StringComparison.OrdinalIgnoreCase); if (!isValid) { ((Controller)filterContext.Controller).ModelState.AddModelError(Field, "验证码不匹配"); } } filterContext.ActionParameters["captchaValid"] = isValid; base.OnActionExecuting(filterContext);//(string)filterContext.HttpContext.GetGlobalResourceObject("LangPack","ValidationCode_Not_Match")); }
public override void ExecuteResult(ControllerContext context) { string guid = context.HttpContext.Request.ServerVariables["Query_String"]; if (guid.Contains("&")) { guid = guid.Split('&')[0]; } var ci = MvcCaptchaImage.GetCachedCaptcha(guid); if (String.IsNullOrEmpty(guid) || ci == null) { context.HttpContext.Response.StatusCode = 404; context.HttpContext.Response.StatusDescription = "Not Found"; context.HttpContext.Response.End(); return; } ci.ResetText(); using (var b = ci.RenderImage()) { b.Save(context.HttpContext.Response.OutputStream, ImageFormat.Gif); } context.HttpContext.Response.Cache.SetNoStore(); context.HttpContext.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache); context.HttpContext.Response.ContentType = "image/gif"; context.HttpContext.Response.StatusCode = 200; context.HttpContext.Response.StatusDescription = "OK"; context.HttpContext.ApplicationInstance.CompleteRequest(); }