Пример #1
0
        public override void ExecuteResult(ControllerContext context)
        {
            string text = context.HttpContext.Request.ServerVariables["Query_String"];

            if (text.Contains("&"))
            {
                text = text.Split(new char[]
                {
                    '&'
                })[0];
            }
            MvcCaptchaImage cachedCaptcha = MvcCaptchaImage.GetCachedCaptcha(text);

            if (string.IsNullOrEmpty(text) || cachedCaptcha == null)
            {
                context.HttpContext.Response.StatusCode        = 404;
                context.HttpContext.Response.StatusDescription = "Not Found";
                context.HttpContext.Response.End();
                return;
            }
            cachedCaptcha.ResetText();
            using (Bitmap bitmap = cachedCaptcha.RenderImage())
            {
                bitmap.Save(context.HttpContext.Response.OutputStream, ImageFormat.Gif);
            }
            context.HttpContext.Response.Cache.SetNoStore();
            context.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.HttpContext.Response.ContentType       = "image/gif";
            context.HttpContext.Response.StatusCode        = 200;
            context.HttpContext.Response.StatusDescription = "OK";
            context.HttpContext.ApplicationInstance.CompleteRequest();
        }
Пример #2
0
        private static MvcHtmlString MvcCaptcha(this HtmlHelper helper, string actionName, string controllerName, MvcCaptchaOptions options)
        {
            if (options == null)
            {
                options = new MvcCaptchaOptions();
            }
            MvcCaptchaImage mvcCaptchaImage = new MvcCaptchaImage(options);
            HttpContext.Current.Session.Add(mvcCaptchaImage.UniqueId, mvcCaptchaImage);
            UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
            StringBuilder stringBuilder = new StringBuilder(1500);
            stringBuilder.Append("\r\n<!--MvcCaptcha 1.1 for ASP.NET MVC 2 RC Copyright:2009-2010 Webdiyer (http://www.webdiyer.com)-->\r\n");
            stringBuilder.Append("<input type=\"hidden\" name=\"_mvcCaptchaGuid\" id=\"_mvcCaptchaGuid\"");
            if (options.DelayLoad)
            {
                stringBuilder.Append("/><script language=\"javascript\" type=\"text/javascript\">if (typeof (jQuery) == \"undefined\") { alert(\"jQuery脚本库未加载,请检查!\"); }");
                stringBuilder.Append("var _mvcCaptchaPrevGuid = null,_mvcCaptchaImgLoaded = false;function _loadMvcCaptchaImage(){");
                stringBuilder.Append("if(!_mvcCaptchaImgLoaded){$.ajax({type:'GET',url:'");
                stringBuilder.Append(urlHelper.Action("MvcCaptchaLoader", "_MvcCaptcha", new RouteValueDictionary
                {

                    {
                        "area",
                        null
                    }
                }));
                stringBuilder.Append("?'+_mvcCaptchaPrevGuid,global:false,success:function(data){_mvcCaptchaImgLoaded=true;");
                stringBuilder.Append("$(\"#_mvcCaptchaGuid\").val(data);_mvcCaptchaPrevGuid=data;$(\"#");
                stringBuilder.Append(options.CaptchaImageContainerId).Append("\").html('");
                stringBuilder.Append(MvcCaptchaHelper.CreateImgTag(urlHelper.Action(actionName, controllerName, new RouteValueDictionary
                {

                    {
                        "area",
                        null
                    }
                }) + "?'+data+'", options, null));
                stringBuilder.Append("');}});} };function _reloadMvcCaptchaImage(){_mvcCaptchaImgLoaded=false;_loadMvcCaptchaImage();};$(function(){");
                stringBuilder.Append("if($(\"#").Append(options.ValidationInputBoxId).Append("\").length==0){alert(\"未能找到验证码输入文本框,请检查ValidationInputBoxId属性是否设置正确!\");}");
                stringBuilder.Append("if($(\"#").Append(options.CaptchaImageContainerId).Append("\").length==0){alert(\"未能找到验证码图片父容器,请检查CaptchaImageContainerId属性是否设置正确!\");}");
                stringBuilder.Append("$(\"#").Append(options.ValidationInputBoxId);
                stringBuilder.Append("\").bind(\"focus\",_loadMvcCaptchaImage)});</script>");
            }
            else
            {
                stringBuilder.AppendFormat(" value=\"{0}\" />", mvcCaptchaImage.UniqueId);
                stringBuilder.Append(MvcCaptchaHelper.CreateImgTag(urlHelper.Action(actionName, controllerName, new RouteValueDictionary
                {

                    {
                        "area",
                        null
                    }
                }) + "?" + mvcCaptchaImage.UniqueId, options, mvcCaptchaImage.UniqueId));
                stringBuilder.Append("<script language=\"javascript\" type=\"text/javascript\">function _reloadMvcCaptchaImage(){var ci=document.getElementById(\"");
                stringBuilder.Append(mvcCaptchaImage.UniqueId);
                stringBuilder.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>");
            }
            stringBuilder.Append("\r\n<!--MvcCaptcha 1.1 for ASP.NET MVC 2 RC Copyright:2009-2010 Webdiyer (http://www.webdiyer.com)-->\r\n");
            return MvcHtmlString.Create(stringBuilder.ToString());
        }
Пример #3
0
        internal Bitmap RenderImage()
        {
            Bitmap bitmap = new Bitmap(this.CaptchaOptions.Width, this.CaptchaOptions.Height, PixelFormat.Format24bppRgb);

            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.SmoothingMode = SmoothingMode.AntiAlias;
                graphics.Clear(Color.White);
                int    num  = 0;
                double num2 = (double)(this.CaptchaOptions.Width / this.CaptchaOptions.TextLength);
                string text = this.Text;
                for (int i = 0; i < text.Length; i++)
                {
                    char c = text[i];
                    using (Font font = this.GetFont())
                    {
                        using (Brush brush = new SolidBrush(this.GetRandomColor()))
                        {
                            Rectangle    rectangle    = new Rectangle(Convert.ToInt32((double)num * num2), 0, Convert.ToInt32(num2), this.CaptchaOptions.Height);
                            GraphicsPath graphicsPath = MvcCaptchaImage.TextPath(c.ToString(), font, rectangle);
                            this.WarpText(graphicsPath, rectangle);
                            graphics.FillPath(brush, graphicsPath);
                            num++;
                        }
                    }
                }
                Rectangle rect = new Rectangle(new Point(0, 0), bitmap.Size);
                this.AddNoise(graphics, rect);
                this.AddLine(graphics, rect);
            }
            return(bitmap);
        }
Пример #4
0
        public ActionResult MvcCaptchaLoader()
        {
            string text = base.Request.ServerVariables["Query_String"];

            if (!string.IsNullOrEmpty(text))
            {
                base.HttpContext.Session.Remove(text);
            }
            MvcCaptchaOptions       mvcCaptchaOptions = new MvcCaptchaOptions();
            MvcCaptchaConfigSection config            = MvcCaptchaConfigSection.GetConfig();

            if (config != null)
            {
                mvcCaptchaOptions.TextChars       = config.TextChars;
                mvcCaptchaOptions.TextLength      = config.TextLength;
                mvcCaptchaOptions.FontWarp        = config.FontWarp;
                mvcCaptchaOptions.BackgroundNoise = config.BackgroundNoise;
                mvcCaptchaOptions.LineNoise       = config.LineNoise;
            }
            MvcCaptchaImage mvcCaptchaImage = new MvcCaptchaImage(mvcCaptchaOptions);

            base.HttpContext.Session.Add(mvcCaptchaImage.UniqueId, mvcCaptchaImage);
            base.HttpContext.Response.Cache.SetNoStore();
            base.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            return(base.Content(mvcCaptchaImage.UniqueId));
        }
Пример #5
0
        private static MvcHtmlString MvcCaptcha(this HtmlHelper helper, string actionName, string controllerName, MvcCaptchaOptions options)
        {
            if (options == null)
            {
                options = new MvcCaptchaOptions();
            }
            MvcCaptchaImage mvcCaptchaImage = new MvcCaptchaImage(options);

            HttpContext.Current.Session.Add(mvcCaptchaImage.UniqueId, mvcCaptchaImage);
            UrlHelper     urlHelper     = new UrlHelper(helper.ViewContext.RequestContext);
            StringBuilder stringBuilder = new StringBuilder(1500);

            stringBuilder.Append("\r\n<!--MvcCaptcha 1.1 for ASP.NET MVC 2 RC Copyright:2009-2010 Webdiyer (http://www.webdiyer.com)-->\r\n");
            stringBuilder.Append("<input type=\"hidden\" name=\"_mvcCaptchaGuid\" id=\"_mvcCaptchaGuid\"");
            if (options.DelayLoad)
            {
                stringBuilder.Append("/><script language=\"javascript\" type=\"text/javascript\">if (typeof (jQuery) == \"undefined\") { alert(\"jQuery脚本库未加载,请检查!\"); }");
                stringBuilder.Append("var _mvcCaptchaPrevGuid = null,_mvcCaptchaImgLoaded = false;function _loadMvcCaptchaImage(){");
                stringBuilder.Append("if(!_mvcCaptchaImgLoaded){$.ajax({type:'GET',url:'");
                stringBuilder.Append(urlHelper.Action("MvcCaptchaLoader", "_MvcCaptcha", new RouteValueDictionary
                {
                    {
                        "area",
                        null
                    }
                }));
                stringBuilder.Append("?'+_mvcCaptchaPrevGuid,global:false,success:function(data){_mvcCaptchaImgLoaded=true;");
                stringBuilder.Append("$(\"#_mvcCaptchaGuid\").val(data);_mvcCaptchaPrevGuid=data;$(\"#");
                stringBuilder.Append(options.CaptchaImageContainerId).Append("\").html('");
                stringBuilder.Append(MvcCaptchaHelper.CreateImgTag(urlHelper.Action(actionName, controllerName, new RouteValueDictionary
                {
                    {
                        "area",
                        null
                    }
                }) + "?'+data+'", options, null));
                stringBuilder.Append("');}});} };function _reloadMvcCaptchaImage(){_mvcCaptchaImgLoaded=false;_loadMvcCaptchaImage();};$(function(){");
                stringBuilder.Append("if($(\"#").Append(options.ValidationInputBoxId).Append("\").length==0){alert(\"未能找到验证码输入文本框,请检查ValidationInputBoxId属性是否设置正确!\");}");
                stringBuilder.Append("if($(\"#").Append(options.CaptchaImageContainerId).Append("\").length==0){alert(\"未能找到验证码图片父容器,请检查CaptchaImageContainerId属性是否设置正确!\");}");
                stringBuilder.Append("$(\"#").Append(options.ValidationInputBoxId);
                stringBuilder.Append("\").bind(\"focus\",_loadMvcCaptchaImage)});</script>");
            }
            else
            {
                stringBuilder.AppendFormat(" value=\"{0}\" />", mvcCaptchaImage.UniqueId);
                stringBuilder.Append(MvcCaptchaHelper.CreateImgTag(urlHelper.Action(actionName, controllerName, new RouteValueDictionary
                {
                    {
                        "area",
                        null
                    }
                }) + "?" + mvcCaptchaImage.UniqueId, options, mvcCaptchaImage.UniqueId));
                stringBuilder.Append("<script language=\"javascript\" type=\"text/javascript\">function _reloadMvcCaptchaImage(){var ci=document.getElementById(\"");
                stringBuilder.Append(mvcCaptchaImage.UniqueId);
                stringBuilder.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>");
            }
            stringBuilder.Append("\r\n<!--MvcCaptcha 1.1 for ASP.NET MVC 2 RC Copyright:2009-2010 Webdiyer (http://www.webdiyer.com)-->\r\n");
            return(MvcHtmlString.Create(stringBuilder.ToString()));
        }
Пример #6
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string          text          = filterContext.HttpContext.Request.Form["_MvcCaptchaGuid"];
            MvcCaptchaImage cachedCaptcha = MvcCaptchaImage.GetCachedCaptcha(text);
            string          text2         = filterContext.HttpContext.Request.Form[this.Field];
            string          text3         = (cachedCaptcha == null) ? string.Empty : cachedCaptcha.Text;

            if (string.IsNullOrEmpty(text2) || string.IsNullOrEmpty(text3) || !string.Equals(text2, text3, StringComparison.OrdinalIgnoreCase))
            {
                ((Controller)filterContext.Controller).ModelState.AddModelError(this.Field, (string)filterContext.HttpContext.GetGlobalResourceObject("LangPack", "ValidationCode_Not_Match"));
            }
            if (!IsRetainSession)
            {
                filterContext.HttpContext.Session.Remove(text);
            }
        }
Пример #7
0
 public ActionResult MvcCaptchaLoader()
 {
     string text = base.Request.ServerVariables["Query_String"];
     if (!string.IsNullOrEmpty(text))
     {
         base.HttpContext.Session.Remove(text);
     }
     MvcCaptchaOptions mvcCaptchaOptions = new MvcCaptchaOptions();
     MvcCaptchaConfigSection config = MvcCaptchaConfigSection.GetConfig();
     if (config != null)
     {
         mvcCaptchaOptions.TextChars = config.TextChars;
         mvcCaptchaOptions.TextLength = config.TextLength;
         mvcCaptchaOptions.FontWarp = config.FontWarp;
         mvcCaptchaOptions.BackgroundNoise = config.BackgroundNoise;
         mvcCaptchaOptions.LineNoise = config.LineNoise;
     }
     MvcCaptchaImage mvcCaptchaImage = new MvcCaptchaImage(mvcCaptchaOptions);
     base.HttpContext.Session.Add(mvcCaptchaImage.UniqueId, mvcCaptchaImage);
     base.HttpContext.Response.Cache.SetNoStore();
     base.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
     return base.Content(mvcCaptchaImage.UniqueId);
 }