Exemplo n.º 1
0
 internal MvcCaptchaImage(MvcCaptchaOptions options)
 {
     CaptchaOptions = options;
     UniqueId       = Guid.NewGuid().ToString("N");
     _rand          = new Random();
     //Text = GenerateRandomText();
 }
Exemplo n.º 2
0
        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));
        }
Exemplo n.º 3
0
        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()));
        }
Exemplo n.º 4
0
        static string CreateImgTag(string url, MvcCaptchaOptions options, string id)
        {
            var sb = new StringBuilder("<a href=\"javascript:_reloadMvcCaptchaImage()\"><img src=\"");

            sb.Append(url);
            sb.Append("\" alt=\"MvcCaptcha\" title=\"刷新图片\" width=\"");
            sb.Append(options.Width);
            sb.Append("\" height=\"");
            sb.Append(options.Height);
            if (!string.IsNullOrEmpty(id))
            {
                sb.Append("\" id=\"").Append(id);
            }
            sb.Append("\" border=\"0\"/></a><a href=\"javascript:_reloadMvcCaptchaImage()\">").Append(options.ReloadLinkText).Append("</a>");
            return(sb.ToString());
        }
Exemplo n.º 5
0
 public static MvcHtmlString MvcCaptcha(this HtmlHelper helper, MvcCaptchaOptions options)
 {
     return(MvcCaptcha(helper, "MvcCaptchaImage", "_MvcCaptcha", options));
 }