Esempio n. 1
0
        /// <summary>
        /// 是否开始使用验证码
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="scenarios">验证码使用场景</param>
        /// <returns></returns>
        public static bool UseCaptcha(VerifyScenarios scenarios = VerifyScenarios.Post, bool isLimitCount = false)
        {
            CaptchaSettings verificationCodeSettings = CaptchaSettings.Instance();

            if (!verificationCodeSettings.EnableCaptcha)
            {
                return(false);
            }
            IUser currentUser = UserContext.CurrentUser;

            if (scenarios == VerifyScenarios.Register || currentUser == null && scenarios == VerifyScenarios.Post)
            {
                return(true);
            }
            //后台登陆
            if (scenarios == VerifyScenarios.Login && currentUser != null)
            {
                return(true);
            }

            if (currentUser == null && scenarios == VerifyScenarios.Post && verificationCodeSettings.EnableAnonymousCaptcha)
            {
                return(true);
            }
            string userName = GetUserName();

            if (scenarios == VerifyScenarios.Login && UserIdToUserNameDictionary.GetUserId(userName) == 0)
            {
                return(false);
            }

            string cacheKey      = GetCacheKey_LimitTryCount(userName, scenarios);
            int?   limitTryCount = cacheService.Get(cacheKey) as int?;

            if (limitTryCount.HasValue &&
                ((scenarios == VerifyScenarios.Login && limitTryCount >= verificationCodeSettings.CaptchaLoginCount) ||
                 (scenarios == VerifyScenarios.Post && limitTryCount >= verificationCodeSettings.CaptchaPostCount)))
            {
                return(true);
            }

            if (isLimitCount)
            {
                if (limitTryCount.HasValue)
                {
                    limitTryCount++;
                }
                else
                {
                    limitTryCount = 1;
                }
                cacheService.Set(cacheKey, limitTryCount, CachingExpirationType.SingleObject);
            }

            return(false);
        }
Esempio n. 2
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. 3
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);
        }