Exemplo n.º 1
0
        protected VerificationCodeImage(Size size, VerificationCodeTextProvider textProvider, CaptchaCharacterSet characterSet, int?maximumCharacters, int?minimumCharacters)
        {
            if (textProvider == null)
            {
                throw new ArgumentNullException("textProvider");
            }

            if (size.Width < MinimumSize.Width || size.Height < MinimumSize.Height)
            {
                throw new ArgumentOutOfRangeException("size", size, "Errors.SizePositiveIntRequired");
            }

            this.textProvider = textProvider;
            if (maximumCharacters.HasValue)
            {
                this.textProvider.MaximumCharacters = maximumCharacters.Value;
            }
            if (minimumCharacters.HasValue)
            {
                this.textProvider.MinimumCharacters = minimumCharacters.Value;
            }
            this.size         = size;
            this.fonts        = new List <Font>(textProvider.Fonts);
            this.textColors   = new List <Color>(textProvider.Colors);
            this.characterSet = characterSet;
        }
        // NOTE: MinimumSize size is only specified when background is null so that ArgumentNullException may be thrown by this constructor
        // after the base constructor is finished.
        protected BackgroundVerificationCodeImage(Image background, VerificationCodeTextProvider textProvider, CaptchaCharacterSet characterSet, int? maximumCharacters, int? minimumCharacters)
            : base((background == null) ? MinimumSize : background.Size, textProvider, characterSet, maximumCharacters, minimumCharacters)
        {
            if (background == null)
                throw new ArgumentNullException("background");

            this.background = background;
        }
        public PartitionedVerificationCodeImage(Image background, VerificationCodeTextProvider textProvider, CaptchaCharacterSet characterSet, int? maximumCharacters, int? minimumCharacters)
            : base(background, textProvider, characterSet, maximumCharacters, minimumCharacters)
        {
            if (textProvider == null)
                throw new ArgumentNullException("textProvider");

            minFontSize = textProvider.MinimumFontSize;
            maxFontSize = textProvider.MaximumFontSize;
        }
Exemplo n.º 4
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();
        }
        public LineNoiseVerificationCodeImage(Size size, VerificationCodeTextProvider textProvider, IEnumerable<Color> colors, CaptchaCharacterSet characterSet, int? maximumCharacters, int? minimumCharacters)
            : base(size, textProvider, characterSet, maximumCharacters, minimumCharacters)
        {
            if (colors == null)
                this.colors = new List<Color>(1);
            else
                this.colors = new List<Color>(colors);

            if (this.colors.Count == 0)
                this.colors.Add(Color.Black);
        }
        /// <summary>
        /// 生成干扰图像
        /// </summary>
        /// <param name="name"></param>
        /// <param name="size"></param>
        /// <param name="textProvider"></param>
        /// <param name="characterSet"></param>
        /// <param name="maximumCharacters"></param>
        /// <param name="minimumCharacters"></param>
        /// <returns></returns>
        public override VerificationCodeImage GenerateAutoInputProtectionImage(string name, Size size, VerificationCodeTextProvider textProvider, CaptchaCharacterSet characterSet, int? maximumCharacters, int? minimumCharacters)
        {
            LineNoiseVerificationCodeImage image = new LineNoiseVerificationCodeImage(size, textProvider, colors, characterSet, maximumCharacters, minimumCharacters);

            // code added after 1.0.0 RTW:
            if (Margin != -1)
                image.Margin = Margin;

            if (MinimumCharacterRotation != -1)
                image.MinimumCharacterRotation = MinimumCharacterRotation;

            if (MaximumCharacterRotation != -1)
                image.MaximumCharacterRotation = MaximumCharacterRotation;

            return image;
        }
Exemplo n.º 7
0
        protected VerificationCodeImage(Size size, VerificationCodeTextProvider textProvider, CaptchaCharacterSet characterSet, int? maximumCharacters, int? minimumCharacters)
        {
            if (textProvider == null)
                throw new ArgumentNullException("textProvider");

            if (size.Width < MinimumSize.Width || size.Height < MinimumSize.Height)
                throw new ArgumentOutOfRangeException("size", size, "Errors.SizePositiveIntRequired");

            this.textProvider = textProvider;
            if (maximumCharacters.HasValue)
                this.textProvider.MaximumCharacters = maximumCharacters.Value;
            if (minimumCharacters.HasValue)
                this.textProvider.MinimumCharacters = minimumCharacters.Value;
            this.size = size;
            this.fonts = new List<Font>(textProvider.Fonts);
            this.textColors = new List<Color>(textProvider.Colors);
            this.characterSet = characterSet;
        }
Exemplo n.º 8
0
        public IActionResult CaptchaHttp()
        {
            string cookieName                = CaptchaSettings.Instance().CaptchaCookieName;
            int    minCharacterCount         = CaptchaSettings.Instance().MinCharacterCount;
            int    maxCharacterCount         = CaptchaSettings.Instance().MaxCharacterCount;
            bool   enableLineNoise           = CaptchaSettings.Instance().EnableLineNoise;
            CaptchaCharacterSet characterSet = CaptchaSettings.Instance().CharacterSet;
            string generatedKey              = string.Empty;
            bool   isAddCooikes              = false;
            //创建或从缓存取验证码
            string publicKey = null;

            if (HttpContext.Request.Cookies[cookieName] != null)
            {
                publicKey = HttpContext.Request.Cookies[cookieName];
            }

            MemoryStream ms = null;

            if (!string.IsNullOrEmpty(publicKey))
            {
                ms = VerificationCodeManager.GetCachedImageStream(publicKey);
            }

            //如果缓存中的数据已经过期移除,就走生成图片流程
            if (ms == null)
            {
                //生成验证码图片并返回验证码并且缓存起来
                string code = VerificationCodeManager.GenerateAndCacheImage(out generatedKey);

                ms = VerificationCodeManager.GetCachedImageStream(generatedKey);
                VerificationCodeManager.CacheCode(HttpContext, code, generatedKey);
                isAddCooikes = true;
            }
            if (isAddCooikes)
            {
                HttpContext.Response.Cookies.Append(cookieName, generatedKey);
            }
            byte[] bytes = ms.ToArray();
            Response.Body.Dispose();
            return(File(bytes, @"image/jpeg"));
        }
Exemplo n.º 9
0
 /// <summary>
 /// When implemented by a derived type, creates an instance of a type that derives from <see cref="AutoInputProtectionImage"/>
 /// that generates an image of the specified <paramref name="size"/>, using the specified <paramref name="name"/> and
 /// <paramref name="textProvider"/>.
 /// </summary>
 /// <remarks>
 /// <para>
 /// Derived types are not required to use the <paramref name="name"/> argument.  This overload is not used by the
 /// <see cref="AutoInputProtectionControl"/> control.
 /// </para>
 /// <para>
 /// A derived type can use the specified <paramref name="name"/> to load or generate a background <see cref="Image"/>
 /// that is passed to <see cref="GenerateImage(Image,AutoInputProtectionTextProvider)"/>, which creates an instance of
 /// <see cref="PartitionedAutoInputProtectionImage"/> that is then returned to the caller immediately or wrapped by a specialized class that
 /// is returned instead.  Alternatively, a derived type may override <see cref="GenerateImage(Image,AutoInputProtectionTextProvider)"/> to
 /// create a custom implementation that is then returned by this method as is.
 /// </para>
 /// <para>
 /// See <see cref="ResourceAutoInputProtectionImageProvider"/> for information about one particular usage of the <paramref name="name"/> argument.
 /// </para>
 /// </remarks>
 /// <seealso cref="AutoInputProtection.GenerateAutoInputProtectionImage"/>
 /// <seealso cref="GenerateRandomAutoInputProtectionImage"/>
 /// <param name="name"><see cref="String"/> that identifies a particular named resource, style or image.</param>
 /// <param name="size">The dimensions of the image.</param>
 /// <param name="textProvider"><see cref="AutoInputProtectionTextProvider"/> implementation to be used by the <see cref="AutoInputProtectionImage"/> implementation that is created.</param>
 /// <returns>An <see cref="AutoInputProtectionImage"/> implementation that can generate an image of the specified <paramref name="size"/> using the specified
 /// <paramref name="textProvider"/>.</returns>
 public abstract override VerificationCodeImage GenerateAutoInputProtectionImage(string name, Size size, VerificationCodeTextProvider textProvider, CaptchaCharacterSet characterSet, int?maximumCharacters, int?minimumCharacters);
Exemplo n.º 10
0
        /// <summary>
        /// Creates an instance of <see cref="PartitionedAutoInputProtectionImage"/> for the specified <paramref name="size"/>
        /// and <paramref name="textProvider"/>.
        /// </summary>
        /// <remarks>
        /// Derived types can override this method to return a type that either composites or derives from
        /// <see cref="PartitionedAutoInputProtectionImage"/>.
        /// </remarks>
        /// <seealso cref="GenerateRandomAutoInputProtectionImage(Size,AutoInputProtectionTextProvider)"/>
        /// <param name="size">The dimensions of the image.</param>
        /// <param name="textProvider">The <see cref="AutoInputProtectionTextProvider"/> implementation that generates the text to be rendered.</param>
        /// <returns>An instance of <see cref="PartitionedAutoInputProtectionImage"/> for the specified <paramref name="size"/>
        /// and <paramref name="textProvider"/>.</returns>
        protected virtual PartitionedVerificationCodeImage GenerateImage(Size size, VerificationCodeTextProvider textProvider, CaptchaCharacterSet characterSet, int?maximumCharacters, int?minimumCharacters)
        {
            PartitionedVerificationCodeImage image = new PartitionedVerificationCodeImage(size, textProvider, characterSet, maximumCharacters, minimumCharacters);

            if (margin != -1)
            {
                image.Margin = margin;
            }

            if (minRotation != -1)
            {
                image.MinimumCharacterRotation = minRotation;
            }

            if (maxRotation != -1)
            {
                image.MaximumCharacterRotation = maxRotation;
            }

            return(image);
        }
Exemplo n.º 11
0
        /// <summary>
        /// 图像的生成和缓存
        /// </summary>
        /// <param name="context">httpcontext</param>
        /// <param name="size">图像大小</param>
        /// <param name="requestTimeoutSeconds">过期时间</param>
        /// <param name="publicKey">生成的publicKey</param>
        /// <param name="characterSet">字符集</param>
        /// <param name="enableLineNoise">是否有干扰线</param>
        /// <param name="maximumCharacters">最大字符长度</param>
        /// <param name="minimumCharacters">最小字符长度</param>
        /// <returns></returns>
        public static VerificationCodeImage GenerateAndCacheImage(HttpContextBase context, Size size, int requestTimeoutSeconds, out string publicKey, CaptchaCharacterSet characterSet, bool enableLineNoise, int?minimumCharacters, int?maximumCharacters)
        {
            MemoryStream          stream;
            VerificationCodeImage aipImage;
            int    attempt = 0;
            string text;

            do
            {
                aipImage = GenerateRandomAutoInputProtectionImage(size, characterSet, maximumCharacters, minimumCharacters);
                text     = aipImage.Text;
            }while (!CreateImageStream(ref attempt, context, aipImage, enableLineNoise, out stream));

            lock (GenerateAndCacheImageLockObj)
            {
                publicKey = GeneratePublicCacheKey(context, text);
                string imageKey = GetCacheKeyForImage(publicKey);
                cacheService.Add(imageKey, stream.ToArray(), new TimeSpan(0, 5, 0));
            }
            return(aipImage);
        }
Exemplo n.º 12
0
        protected BackgroundVerificationCodeImage(Image background, VerificationCodeTextProvider textProvider, CaptchaCharacterSet characterSet, int?maximumCharacters, int?minimumCharacters)
        // NOTE: MinimumSize size is only specified when background is null so that ArgumentNullException may be thrown by this constructor
        // after the base constructor is finished.
            : base((background == null) ? MinimumSize : background.Size, textProvider, characterSet, maximumCharacters, minimumCharacters)
        {
            if (background == null)
            {
                throw new ArgumentNullException("background");
            }

            this.background = background;
        }
Exemplo n.º 13
0
        private static VerificationCodeImage GenerateRandomAutoInputProtectionImage(Size size, CaptchaCharacterSet characterSet, int? maximumCharacters, int? minimumCharacters)
        {
            if (size.Width < 1 || size.Height < 1)
                throw new ArgumentOutOfRangeException("size", size, "Errors.SizePositiveIntRequired");

            EnsureProviders();
            VerificationCodeImage image = null;
            lock (VerificationCodeImageLockObj)
            {
                image = imageProvider.GenerateRandomAutoInputProtectionImage(size, textProvider, characterSet, maximumCharacters, minimumCharacters);
            }
            image.filters = filterProviders;

            return image;
        }
        /// <summary>
        /// Creates an instance of <see cref="PartitionedAutoInputProtectionImage"/> for the specified <paramref name="size"/>
        /// and <paramref name="textProvider"/>.
        /// </summary>
        /// <remarks>
        /// Derived types can override this method to return a type that either composites or derives from 
        /// <see cref="PartitionedAutoInputProtectionImage"/>.
        /// </remarks>
        /// <seealso cref="GenerateRandomAutoInputProtectionImage(Size,AutoInputProtectionTextProvider)"/>
        /// <param name="size">The dimensions of the image.</param>
        /// <param name="textProvider">The <see cref="AutoInputProtectionTextProvider"/> implementation that generates the text to be rendered.</param>
        /// <returns>An instance of <see cref="PartitionedAutoInputProtectionImage"/> for the specified <paramref name="size"/>
        /// and <paramref name="textProvider"/>.</returns>
        protected virtual PartitionedVerificationCodeImage GenerateImage(Size size, VerificationCodeTextProvider textProvider, CaptchaCharacterSet characterSet, int? maximumCharacters, int? minimumCharacters)
        {
            PartitionedVerificationCodeImage image = new PartitionedVerificationCodeImage(size, textProvider, characterSet, maximumCharacters, minimumCharacters);

            if (margin != -1)
                image.Margin = margin;

            if (minRotation != -1)
                image.MinimumCharacterRotation = minRotation;

            if (maxRotation != -1)
                image.MaximumCharacterRotation = maxRotation;

            return image;
        }
 /// <summary>
 /// 生成图像
 /// </summary>
 /// <param name="size"></param>
 /// <param name="textProvider"></param>
 /// <param name="characterSet"></param>
 /// <param name="maximumCharacters"></param>
 /// <param name="minimumCharacters"></param>
 /// <returns></returns>
 public override VerificationCodeImage GenerateRandomAutoInputProtectionImage(Size size, VerificationCodeTextProvider textProvider, CaptchaCharacterSet characterSet, int? maximumCharacters, int? minimumCharacters)
 {
     return GenerateAutoInputProtectionImage(null, size, textProvider, characterSet, maximumCharacters, minimumCharacters);
 }
 public LineNoiseVerificationCodeImage(Size size, VerificationCodeTextProvider textProvider, CaptchaCharacterSet characterSet, int? maximumCharacters, int? minimumCharacters)
     : base(size, textProvider, characterSet, maximumCharacters, minimumCharacters)
 {
     colors = new List<Color>(1);
     colors.Add(Color.Black);
 }
 /// <summary>
 /// 生成字符集
 /// </summary>
 /// <param name="characterSet"></param>
 /// <returns></returns>
 public override string GenerateRandomAutoInputProtectionText(CaptchaCharacterSet characterSet)
 {
     return VerificationCodeRandomString.Create(MinimumCharacters, MaximumCharacters, characterSet, null);
 }
        public PartitionedVerificationCodeImage(Image background, VerificationCodeTextProvider textProvider, CaptchaCharacterSet characterSet, int?maximumCharacters, int?minimumCharacters)
            : base(background, textProvider, characterSet, maximumCharacters, minimumCharacters)
        {
            if (textProvider == null)
            {
                throw new ArgumentNullException("textProvider");
            }

            minFontSize = textProvider.MinimumFontSize;
            maxFontSize = textProvider.MaximumFontSize;
        }
Exemplo n.º 19
0
        public LineNoiseVerificationCodeImage(Size size, VerificationCodeTextProvider textProvider, IEnumerable <Color> colors, CaptchaCharacterSet characterSet, int?maximumCharacters, int?minimumCharacters)
            : base(size, textProvider, characterSet, maximumCharacters, minimumCharacters)
        {
            if (colors == null)
            {
                this.colors = new List <Color>(1);
            }
            else
            {
                this.colors = new List <Color>(colors);
            }

            if (this.colors.Count == 0)
            {
                this.colors.Add(Color.Black);
            }
        }
Exemplo n.º 20
0
 public LineNoiseVerificationCodeImage(Size size, VerificationCodeTextProvider textProvider, CaptchaCharacterSet characterSet, int?maximumCharacters, int?minimumCharacters)
     : base(size, textProvider, characterSet, maximumCharacters, minimumCharacters)
 {
     colors = new List <Color>(1);
     colors.Add(Color.Black);
 }
Exemplo n.º 21
0
 public abstract string GenerateRandomAutoInputProtectionText(CaptchaCharacterSet characterSet);
Exemplo n.º 22
0
        /// <summary>
        /// Returns a string of random characters from the specified set of charactersAllowed, excluding
        /// the characters in charactersNotAllowed, with a length that is between the
        /// specified minLengthand maxLength values, inclusive.
        /// </summary>
        public static string Create(int minLength, int maxLength, CaptchaCharacterSet charactersAllowed, string charactersNotAllowed)
        {
            if (minLength < 0)
            {
                throw new ArgumentOutOfRangeException("minLength", minLength, "Errors.PositiveIntOrZeroRequired");
            }

            if (maxLength < minLength)
            {
                throw new ArgumentOutOfRangeException("maxLength", maxLength,
                                                      string.Format(System.Globalization.CultureInfo.CurrentUICulture, "Errors.RandomString_MaxLessThanMin", minLength));
            }

            List <int> validCharSetValues = new List <int>((int[])Enum.GetValues(typeof(CaptchaCharacterSet)));

            for (int i = 0; i < validCharSetValues.Count;)
            // for each index in the array of CharacterSet constant values remove
            // the ones that aren't bit-aligned or aren't selected in charSets
            {
                int csVal = validCharSetValues[i];

                if (!IsBitAligned(csVal) || (csVal & (int)charactersAllowed) == 0)
                {
                    validCharSetValues.RemoveAt(i);
                    continue;
                }

                i++;
            }

            Random rnd = new Random();

            int length = rnd.Next(minLength, maxLength + 1);

            if (length == 0)
            {
                return(string.Empty);
            }

            StringBuilder value = new StringBuilder(length);

            if (!string.IsNullOrEmpty(charactersNotAllowed))
            {
                for (int i = 0; i < length; i++)
                // for each index in the string value generate a random character
                {
                    CaptchaCharacterSet validCharacters = (CaptchaCharacterSet)validCharSetValues[rnd.Next(0, validCharSetValues.Count)];

                    Point range = ranges[validCharacters];

                    char c = (char)rnd.Next(range.X, range.Y + 1);

                    if (charactersNotAllowed.IndexOf(c) == -1)          // case-sensitive comparison
                    {
                        value.Append(c);
                    }
                    else
                    {
                        // the current character must be replaced because it has been excluded
                        i--;
                    }
                }
            }
            else
            {
                for (int i = 0; i < length; i++)
                // for each index in the string value generate a random character
                {
                    // randomly choose a set of characters and get the character range for that set
                    Point range = ranges[(CaptchaCharacterSet)validCharSetValues[rnd.Next(0, validCharSetValues.Count)]];

                    value.Append((char)rnd.Next(range.X, range.Y + 1));
                }
            }

            return(value.ToString());
        }
 /// <summary>
 /// When implemented by a derived type, creates an instance of a type that derives from <see cref="AutoInputProtectionImage"/>
 /// that generates an image of the specified <paramref name="size"/> using the specified <paramref name="textProvider"/>.
 /// </summary>
 /// <remarks>
 /// <para>
 /// This overload is used by the <see cref="AutoInputProtectionControl"/> control.
 /// </para>
 /// <para>
 /// A derived type can call <see cref="GenerateImage(Size,AutoInputProtectionTextProvider)"/> to create an instance of 
 /// <see cref="PartitionedAutoInputProtectionImage"/>, which can then be returned to the caller immediately or it can be wrapped by a specialied class that 
 /// is returned instead.  Alternatively, a derived type may override <see cref="GenerateImage(Size,AutoInputProtectionTextProvider)"/> to create a custom 
 /// implementation that is then returned by this method as is.
 /// </para>
 /// </remarks>
 /// <seealso cref="AutoInputProtection.GenerateAutoInputProtectionImage"/>
 /// <seealso cref="GenerateAutoInputProtectionImage"/>
 /// <param name="size">The dimensions of the image.</param>
 /// <param name="textProvider"><see cref="AutoInputProtectionTextProvider"/> implementation to be used by the <see cref="AutoInputProtectionImage"/> implementation that is created.</param>
 /// <returns>An <see cref="AutoInputProtectionImage"/> implementation that can generate an image of the specified <paramref name="size"/> using the specified
 /// <paramref name="textProvider"/>.</returns>
 public abstract override VerificationCodeImage GenerateRandomAutoInputProtectionImage(Size size, VerificationCodeTextProvider textProvider, CaptchaCharacterSet characterSet, int? maximumCharacters, int? minimumCharacters);
 protected BackgroundVerificationCodeImage(Size size, VerificationCodeTextProvider textProvider, CaptchaCharacterSet characterSet, int? maximumCharacters, int? minimumCharacters)
     : base(size, textProvider, characterSet, maximumCharacters, minimumCharacters)
 {
 }
Exemplo n.º 25
0
        /// <summary>
        /// Returns a string of random characters from the specified set of charactersAllowed, excluding 
        /// the characters in charactersNotAllowed, with a length that is between the 
        /// specified minLengthand maxLength values, inclusive.
        /// </summary>
        public static string Create(int minLength, int maxLength, CaptchaCharacterSet charactersAllowed, string charactersNotAllowed)
        {
            if (minLength < 0)
                throw new ArgumentOutOfRangeException("minLength", minLength, "Errors.PositiveIntOrZeroRequired");

            if (maxLength < minLength)
                throw new ArgumentOutOfRangeException("maxLength", maxLength,
                    string.Format(System.Globalization.CultureInfo.CurrentUICulture, "Errors.RandomString_MaxLessThanMin", minLength));

            List<int> validCharSetValues = new List<int>((int[])Enum.GetValues(typeof(CaptchaCharacterSet)));

            for (int i = 0; i < validCharSetValues.Count; )
            // for each index in the array of CharacterSet constant values remove
            // the ones that aren't bit-aligned or aren't selected in charSets
            {
                int csVal = validCharSetValues[i];

                if (!IsBitAligned(csVal) || (csVal & (int)charactersAllowed) == 0)
                {
                    validCharSetValues.RemoveAt(i);
                    continue;
                }

                i++;
            }

            Random rnd = new Random();

            int length = rnd.Next(minLength, maxLength + 1);

            if (length == 0)
                return string.Empty;

            StringBuilder value = new StringBuilder(length);

            if (!string.IsNullOrEmpty(charactersNotAllowed))
            {
                for (int i = 0; i < length; i++)
                // for each index in the string value generate a random character
                {
                    CaptchaCharacterSet validCharacters = (CaptchaCharacterSet)validCharSetValues[rnd.Next(0, validCharSetValues.Count)];

                    Point range = ranges[validCharacters];

                    char c = (char)rnd.Next(range.X, range.Y + 1);

                    if (charactersNotAllowed.IndexOf(c) == -1)		// case-sensitive comparison
                        value.Append(c);
                    else
                        // the current character must be replaced because it has been excluded
                        i--;
                }
            }
            else
            {
                for (int i = 0; i < length; i++)
                // for each index in the string value generate a random character
                {
                    // randomly choose a set of characters and get the character range for that set
                    Point range = ranges[(CaptchaCharacterSet)validCharSetValues[rnd.Next(0, validCharSetValues.Count)]];

                    value.Append((char)rnd.Next(range.X, range.Y + 1));
                }
            }

            return value.ToString();
        }
Exemplo n.º 26
0
        /// <summary>
        /// 图像的生成和缓存
        /// </summary>
        /// <param name="context">httpcontext</param>
        /// <param name="size">图像大小</param>
        /// <param name="requestTimeoutSeconds">过期时间</param>
        /// <param name="publicKey">生成的publicKey</param>
        /// <param name="characterSet">字符集</param>
        /// <param name="enableLineNoise">是否有干扰线</param>
        /// <param name="maximumCharacters">最大字符长度</param>
        /// <param name="minimumCharacters">最小字符长度</param>
        /// <returns></returns>
        public static VerificationCodeImage GenerateAndCacheImage(HttpContextBase context, Size size, int requestTimeoutSeconds, out string publicKey, CaptchaCharacterSet characterSet, bool enableLineNoise, int? minimumCharacters, int? maximumCharacters)
        {
            MemoryStream stream;
            VerificationCodeImage aipImage;
            int attempt = 0;
            string text;

            do
            {
                aipImage = GenerateRandomAutoInputProtectionImage(size, characterSet, maximumCharacters, minimumCharacters);
                text = aipImage.Text;
            }
            while (!CreateImageStream(ref attempt, context, aipImage, enableLineNoise, out stream));

            lock (GenerateAndCacheImageLockObj)
            {
                publicKey = GeneratePublicCacheKey(context, text);
                string imageKey = GetCacheKeyForImage(publicKey);
                cacheService.Add(imageKey, stream.ToArray(), new TimeSpan(0, 5, 0));
            }
            return aipImage;
        }
        public static VerificationCodeImage GenerateAndCacheImage(HttpContextBase context, Size size, int timeout,
          out string key, CaptchaCharacterSet captchaCharacterSet, bool enableLineNoise, int minmunCharacter,
          int maxmunCharacter)
        {
            MemoryStream stream;
            VerificationCodeImage apiImage;
            int attempt = 0;
            string text;
            do
            {
                apiImage = GenerateRandomAutoProtectionImage(size, captchaCharacterSet, maxmunCharacter, minmunCharacter);
                text = apiImage.Text;
            } while (CreateImageStream(ref attempt, context, apiImage, enableLineNoise, out stream));
            lock (VerificationCodeImageLockObj)
            {
                key = GeneratePublishCacheKey(context, text);
                string imagekey = GetCacheKeyForImage(key);
                //缓存5分钟
                //cacheService.Add(imagekey,stream.ToArray(),new TimeSpan(0,5,0));
            }

            return apiImage;
        }
 /// <summary>
 /// 生成字符集
 /// </summary>
 /// <param name="characterSet"></param>
 /// <returns></returns>
 public override string GenerateRandomAutoInputProtectionText(CaptchaCharacterSet characterSet)
 {
     return(VerificationCodeRandomString.Create(MinimumCharacters, MaximumCharacters, characterSet, null));
 }
Exemplo n.º 29
0
 /// <summary>
 /// When implemented by a derived type, creates an instance of a type that derives from <see cref="AutoInputProtectionImage"/>
 /// that generates an image of the specified <paramref name="size"/> using the specified <paramref name="textProvider"/>.
 /// </summary>
 /// <remarks>
 /// This overload is used by the <see cref="AutoInputProtectionControl"/> control.
 /// </remarks>
 /// <seealso cref="AutoInputProtection.GenerateAutoInputProtectionImage"/>
 /// <seealso cref="GenerateAutoInputProtectionImage"/>
 /// <param name="size">The dimensions of the image.</param>
 /// <param name="textProvider"><see cref="AutoInputProtectionTextProvider"/> implementation to be used by the <see cref="AutoInputProtectionImage"/> implementation that is created.</param>
 /// <returns>An <see cref="AutoInputProtectionImage"/> implementation that can generate an image of the specified <paramref name="size"/> using the specified
 /// <paramref name="textProvider"/>.</returns>
 public abstract VerificationCodeImage GenerateRandomAutoInputProtectionImage(Size size, VerificationCodeTextProvider textProvider, CaptchaCharacterSet characterSet, int?maximumCharacters, int?minimumCharacters);
        /// <summary>
        /// 生成干扰图像
        /// </summary>
        /// <param name="name"></param>
        /// <param name="size"></param>
        /// <param name="textProvider"></param>
        /// <param name="characterSet"></param>
        /// <param name="maximumCharacters"></param>
        /// <param name="minimumCharacters"></param>
        /// <returns></returns>
        public override VerificationCodeImage GenerateAutoInputProtectionImage(string name, Size size, VerificationCodeTextProvider textProvider, CaptchaCharacterSet characterSet, int?maximumCharacters, int?minimumCharacters)
        {
            LineNoiseVerificationCodeImage image = new LineNoiseVerificationCodeImage(size, textProvider, colors, characterSet, maximumCharacters, minimumCharacters);

            // code added after 1.0.0 RTW:
            if (Margin != -1)
            {
                image.Margin = Margin;
            }

            if (MinimumCharacterRotation != -1)
            {
                image.MinimumCharacterRotation = MinimumCharacterRotation;
            }

            if (MaximumCharacterRotation != -1)
            {
                image.MaximumCharacterRotation = MaximumCharacterRotation;
            }

            return(image);
        }
        /// <summary>
        /// 创建验证码实体对象
        /// </summary>
        /// <param name="size"></param>
        /// <param name="characterSet"></param>
        /// <param name="maxmunCharacter"></param>
        /// <param name="minmunCharacter"></param>
        /// <returns></returns>
        private static VerificationCodeImage GenerateRandomAutoProtectionImage(Size size,
            CaptchaCharacterSet characterSet, int maxmunCharacter, int minmunCharacter)
        {
            if (size.Width < 1 || size.Height < 1) throw new ArgumentOutOfRangeException("绘制验证码size的不正确");

            EnsureProviders();

            VerificationCodeImage image = null;
            lock (VerificationCodeImageLockObj)
            {
                image = imageProvider.GenerateRandomAutoInputProtectionImage(size,textProvider,characterSet,maxmunCharacter,minmunCharacter);
            }

            image.filters = FilterProviderCollection;

            return image;
        }
 /// <summary>
 /// 生成图像
 /// </summary>
 /// <param name="size"></param>
 /// <param name="textProvider"></param>
 /// <param name="characterSet"></param>
 /// <param name="maximumCharacters"></param>
 /// <param name="minimumCharacters"></param>
 /// <returns></returns>
 public override VerificationCodeImage GenerateRandomAutoInputProtectionImage(Size size, VerificationCodeTextProvider textProvider, CaptchaCharacterSet characterSet, int?maximumCharacters, int?minimumCharacters)
 {
     return(GenerateAutoInputProtectionImage(null, size, textProvider, characterSet, maximumCharacters, minimumCharacters));
 }
Exemplo n.º 33
0
        private static VerificationCodeImage GenerateRandomAutoInputProtectionImage(Size size, CaptchaCharacterSet characterSet, int?maximumCharacters, int?minimumCharacters)
        {
            if (size.Width < 1 || size.Height < 1)
            {
                throw new ArgumentOutOfRangeException("size", size, "Errors.SizePositiveIntRequired");
            }

            EnsureProviders();
            VerificationCodeImage image = null;

            lock (VerificationCodeImageLockObj)
            {
                image = imageProvider.GenerateRandomAutoInputProtectionImage(size, textProvider, characterSet, maximumCharacters, minimumCharacters);
            }
            image.filters = filterProviders;

            return(image);
        }
Exemplo n.º 34
0
 protected BackgroundVerificationCodeImage(Size size, VerificationCodeTextProvider textProvider, CaptchaCharacterSet characterSet, int?maximumCharacters, int?minimumCharacters)
     : base(size, textProvider, characterSet, maximumCharacters, minimumCharacters)
 {
 }