private async Task <bool> isTokenValid(string token)
        {
            try
            {
                var item = await context.Tokens.Where(t => t.Value.Equals(token) && t.Expires > DateTime.Now && t.Used == false).FirstOrDefaultAsync();

                if (item != null)
                {
                    // update token if valid then return valid
                    item.Used = true;
                    int result = context.SaveChanges();
                    return(Convert.ToBoolean(result));
                }
            }
            catch (System.Exception e)
            {
                exceptionManager.DoException(e);
            }
            return(false);
        }
Beispiel #2
0
        /// <summary>
        /// generate captcha image
        /// </summary>
        /// <param name="size">alphanumeric length of the captcha</param>
        /// <returns>image properties and image binary data</returns>
        private async Task <CaptchaImage> generateCaptcha(int size)
        {
            try
            {
                int width  = size * 30;
                int height = width / 4;
                var b      = new Bitmap(width, height);
                var g      = Graphics.FromImage(b);

                //create brush and rectangle
                var brush = new HatchBrush(HatchStyle.SmallConfetti, Color.LightSteelBlue, Color.White);
                g.FillRectangle(brush, 0, 0, b.Width, b.Height);
                float emSize = width / size;
                var   fnt    = new Font("Arial", emSize, FontStyle.Italic);

                // generate truly random captcha value
                string value = generateRandomCaptchaValue(size);
                while (string.IsNullOrEmpty(value))
                {
                    value = generateRandomCaptchaValue(size);
                }

                // write the generated value on the image
                g.DrawString(value, fnt, Brushes.Coral, 0, 0, StringFormat.GenericTypographic);

                // generate random noise on the image
                brush = new HatchBrush(HatchStyle.LargeConfetti, Color.SlateBlue, Color.SlateGray);
                fillRandomNoise(g, brush, width, height);

                // draw random lines on the image
                Point[] iP = getRandomPoints(width, height);
                for (int i = 0; i < 3; i++)
                {
                    Brush brs = Brushes.BlueViolet;
                    if (i % 3 == 1)
                    {
                        brs = Brushes.DodgerBlue;
                    }
                    if (i % 3 == 2)
                    {
                        brs = Brushes.MediumVioletRed;
                    }
                    Pen pn = new Pen(brs, 2);
                    g.DrawBezier(pn, iP[i * 4], iP[i * 4 + 1], iP[i * 4 + 2], iP[i * 4 + 3]);
                }

                // create image
                byte[] bBuffer = (byte[])System.ComponentModel.TypeDescriptor.GetConverter(b).ConvertTo(b, typeof(byte[]));

                // add image properties to database
                var guid = await saveCaptcha(value);

                // return image with properties
                if (guid.HasValue)
                {
                    return new CaptchaImage()
                           {
                               ID = guid.Value, Value = value, Data = bBuffer, MimeType = "image/jpg"
                           }
                }
                ;
            }
            catch (System.Exception e)
            {
                exceptionManager.DoException(e);
            }
            return(null);
        }