forked from siteserver/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageValidateCode.cs
More file actions
80 lines (65 loc) · 2.88 KB
/
PageValidateCode.cs
File metadata and controls
80 lines (65 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Collections.Specialized;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.Web.UI;
using SiteServer.Utils;
using SiteServer.BackgroundPages.Core;
namespace SiteServer.BackgroundPages
{
public class PageValidateCode : Page
{
private static readonly Color[] Colors = { Color.FromArgb(37, 72, 91), Color.FromArgb(68, 24, 25), Color.FromArgb(17, 46, 2), Color.FromArgb(70, 16, 100), Color.FromArgb(24, 88, 74) };
public static string GetRedirectUrl(string cookieName)
{
return PageUtils.GetSiteServerUrl(nameof(PageValidateCode), new NameValueCollection
{
{"cookieName", cookieName}
});
}
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);
}
public void ValidateCode(string validateCode)
{
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(validateCode, 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();
}
}
}