/// <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 ActionResult MvcCaptchaLoader() { string prevGuid = Request.ServerVariables["Query_String"]; if (!string.IsNullOrEmpty(prevGuid)) { HttpContext.Session.Remove(prevGuid); } var options = new MvcCaptchaOptions() { }; var config = MvcCaptchaConfigSection.GetConfig(); if (config != null) { options.TextChars = config.TextChars; options.TextLength = config.TextLength; options.FontWarp = config.FontWarp; options.BackgroundNoise = config.BackgroundNoise; options.LineNoise = config.LineNoise; } var image = new MvcCaptchaImage(options); HttpContext.Session.Add( image.UniqueId, image); HttpContext.Response.Cache.SetNoStore(); HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache); return(Content(image.UniqueId)); }
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(); }
private static MvcHtmlString MvcCaptcha(this HtmlHelper helper, string actionName, string controllerName, MvcCaptchaOptions options) { if (options == null) { options = new MvcCaptchaOptions(); } var image = new MvcCaptchaImage(options); HttpContext.Current.Session.Add( image.UniqueId, image); var url = new UrlHelper(helper.ViewContext.RequestContext); var sb = new StringBuilder(1500); const string copyrightText = "\r\n<!--MvcCaptcha-->\r\n"; // sb.Append(copyrightText); sb.Append("<input type=\"hidden\" name=\"_mvcCaptchaGuid\" id=\"_mvcCaptchaGuid\""); if (options.DelayLoad) { sb.Append("/><script language=\"javascript\" type=\"text/javascript\">if (typeof (jQuery) == \"undefined\") { alert(\"jQuery脚本库未加载,请检查!\"); }"); sb.Append("var _mvcCaptchaPrevGuid = null,_mvcCaptchaImgLoaded = false;function _loadMvcCaptchaImage(){"); sb.Append("if(!_mvcCaptchaImgLoaded){$.ajax({type:'GET',url:'"); sb.Append(url.Action("MvcCaptchaLoader", "_MvcCaptcha", new RouteValueDictionary { { "area", null } })); sb.Append("?'+_mvcCaptchaPrevGuid,global:false,success:function(data){_mvcCaptchaImgLoaded=true;"); sb.Append("$(\"#_mvcCaptchaGuid\").val(data);_mvcCaptchaPrevGuid=data;$(\"#"); sb.Append(options.CaptchaImageContainerId).Append("\").html('"); sb.Append(CreateImgTag(url.Action(actionName, controllerName, new RouteValueDictionary { { "area", null } }) + "?'+data+'", options, null)); sb.Append("');}});} };function _reloadMvcCaptchaImage(){_mvcCaptchaImgLoaded=false;_loadMvcCaptchaImage();};$(function(){"); sb.Append("if($(\"#").Append(options.ValidationInputBoxId).Append("\").length==0){alert(\"未能找到验证码输入文本框,请检查ValidationInputBoxId属性是否设置正确!\");}"); sb.Append("if($(\"#").Append(options.CaptchaImageContainerId).Append("\").length==0){alert(\"未能找到验证码图片父容器,请检查CaptchaImageContainerId属性是否设置正确!\");}"); sb.Append("$(\"#").Append(options.ValidationInputBoxId); sb.Append("\").bind(\"focus\",_loadMvcCaptchaImage)});</script>"); } else { sb.AppendFormat(" value=\"{0}\" />", image.UniqueId); sb.Append(CreateImgTag(url.Action(actionName, controllerName, new RouteValueDictionary { { "area", null } }) + "?" + image.UniqueId, options, image.UniqueId)); sb.Append("<script language=\"javascript\" type=\"text/javascript\">function _reloadMvcCaptchaImage(){var ci=document.getElementById(\""); sb.Append(image.UniqueId); sb.Append("\");var sl=ci.src.length;if(ci.src.indexOf(\"&\")>-1)sl=ci.src.indexOf(\"&\");ci.src=ci.src.substr(0,sl)+\"&\"+(new Date().valueOf());}</script>"); } // sb.Append(copyrightText); return(MvcHtmlString.Create(sb.ToString())); }