Esempio n. 1
0
        public void ProcessRequest(HttpContext context)
        {
            HttpContextBase currentContext = new HttpContextWrapper(context);
            bool            isremove       = false;

            if (!string.IsNullOrEmpty(context.Request.QueryString["isremove"]))
            {
                bool.TryParse(context.Request.QueryString["isremove"], out isremove);
            }

            string cookieName                = CaptchaSettings.Instance().CaptchaCookieName;
            bool   enableLineNoise           = CaptchaSettings.Instance().EnableLineNoise;
            CaptchaCharacterSet characterSet = CaptchaSettings.Instance().CharacterSet;
            int    minCharacterCount         = CaptchaSettings.Instance().MinCharacterCount;
            int    maxCharacterCount         = CaptchaSettings.Instance().MaxCharacterCount;
            string generatedKey              = string.Empty;
            bool   addCooikes                = false;
            //创建或从缓存取验证码
            string key = null;

            if (context.Request.Cookies[cookieName] != null)
            {
                key = context.Request.Cookies[cookieName].Value;
            }
            if (isremove && !string.IsNullOrEmpty(key))
            {
                VerificationCodeManager.GetCachedTextAndForceExpire(currentContext, getCurrentLevelKey(key));
            }

            System.IO.MemoryStream ms = null;
            if (!string.IsNullOrEmpty(key))
            {
                ms = VerificationCodeManager.GetCachedImageStream(getCurrentLevelKey(key));
            }

            if (ms == null)
            {
                Size size = new Size(85, 30);
                VerificationCodeImage image = VerificationCodeManager.GenerateAndCacheImage(currentContext, size, 300, out generatedKey, characterSet, enableLineNoise, minCharacterCount, maxCharacterCount);

                ms = VerificationCodeManager.GetCachedImageStream(getCurrentLevelKey(generatedKey));
                VerificationCodeManager.CacheText(currentContext, image.Text, getCurrentLevelKey(generatedKey), false, 300);
                addCooikes = true;
            }
            if (addCooikes)
            {
                HttpCookie cookie = new HttpCookie(cookieName, generatedKey);
                context.Response.Cookies.Add(cookie);
            }
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.ContentType = "image/Jpeg";
            context.Response.BinaryWrite(ms.ToArray());
            //context.Response.Flush();
            context.Response.End();
        }
Esempio n. 2
0
        /// <summary>
        /// 验证码是否输入正确
        /// </summary>
        /// <param name="filterContext"></param>
        /// <returns></returns>
        public bool IsCaptchaValid(ActionExecutingContext filterContext)
        {
            ControllerBase controllerBase = filterContext.Controller;
            string         captchaText    = controllerBase.ControllerContext.HttpContext.Request.Form[_captchaInputName];

            if (string.IsNullOrEmpty(captchaText))
            {
                return(false);
            }

            string     cookieName = CaptchaSettings.Instance().CaptchaCookieName;
            HttpCookie coookie    = filterContext.HttpContext.Request.Cookies[cookieName];

            string cookieCaptcha = string.Empty;

            if (coookie != null)
            {
                if (!string.IsNullOrEmpty(coookie.Value))
                {
                    try
                    {
                        cookieCaptcha = VerificationCodeManager.GetCachedTextAndForceExpire(filterContext.HttpContext, coookie.Value);
                    }
                    catch { }
                }
            }

            //从cookie未获取验证码时,提供一个随机数
            if (cookieCaptcha == null)
            {
                cookieCaptcha = DateTime.UtcNow.Ticks.ToString();
            }

            if (!string.IsNullOrEmpty(captchaText) &&
                !captchaText.Equals(cookieCaptcha, StringComparison.CurrentCultureIgnoreCase))
            {
                return(false);
            }
            return(true);
        }