Пример #1
0
        public void Get(string name)
        {
            var response = HttpContext.Current.Response;

            var code = VcManager.CreateValidateCode();

            if (CacheUtils.Exists($"SiteServer.API.Controllers.V1.CaptchaController.{code}"))
            {
                code = VcManager.CreateValidateCode();
            }

            CookieUtils.SetCookie("SS-" + name, code, DateTime.Now.AddMinutes(10));

            response.BufferOutput = true;                                //特别注意
            response.Cache.SetExpires(DateTime.Now.AddMilliseconds(-1)); //特别注意
            response.Cache.SetCacheability(HttpCacheability.NoCache);    //特别注意
            response.AppendHeader("Pragma", "No-Cache");                 //特别注意
            response.ContentType = "image/png";

            var validateimage = new Bitmap(130, 53, PixelFormat.Format32bppRgb);

            var r      = new Random();
            var colors = Colors[r.Next(0, 5)];

            var g = Graphics.FromImage(validateimage);

            g.FillRectangle(new SolidBrush(Color.FromArgb(240, 243, 248)), 0, 0, 200, 200);                                                          //矩形框
            g.DrawString(code, new Font(FontFamily.GenericSerif, 28, FontStyle.Bold | FontStyle.Italic), new SolidBrush(colors), new PointF(14, 3)); //字体/颜色

            var random = new Random();

            for (var i = 0; i < 25; i++)
            {
                var x1 = random.Next(validateimage.Width);
                var x2 = random.Next(validateimage.Width);
                var y1 = random.Next(validateimage.Height);
                var y2 = random.Next(validateimage.Height);

                g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
            }

            for (var i = 0; i < 100; i++)
            {
                var x = random.Next(validateimage.Width);
                var y = random.Next(validateimage.Height);

                validateimage.SetPixel(x, y, Color.FromArgb(random.Next()));
            }

            g.Save();
            var ms = new MemoryStream();

            validateimage.Save(ms, ImageFormat.Png);
            response.ClearContent();
            response.BinaryWrite(ms.ToArray());
            response.End();
        }
Пример #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            var cookieName = Request.QueryString["cookieName"];

            var validateCode = VcManager.CreateValidateCode();

            CookieUtils.SetCookie(cookieName, validateCode, DateTime.Now.AddDays(1));

            Response.BufferOutput = true;                                //特别注意
            Response.Cache.SetExpires(DateTime.Now.AddMilliseconds(-1)); //特别注意
            Response.Cache.SetCacheability(HttpCacheability.NoCache);    //特别注意
            Response.AppendHeader("Pragma", "No-Cache");                 //特别注意
            Response.ContentType = "image/png";
            ValidateCode(validateCode);
        }