随机笔记
public interface IRandomTextGenerator
{
/// <summary>
/// Gets or sets the settings.
/// </summary>
/// <value>The settings.</value>
RandomTextGeneratorSettings Settings { get; set; }
/// <summary>
/// Generates this instance.
/// </summary>
/// <returns></returns>
string Generate();
}
/// <summary>
/// Settings for the random text generator.
/// </summary>
public class RandomTextGeneratorSettings
{
/// <summary>
/// Gets or sets the length of random charachters to generate
/// </summary>
/// <value>The length.</value>
public int Length { get; set; }
/// <summary>
/// Gets or sets the allowed chars.
/// </summary>
/// <value>The allowed chars.</value>
public string AllowedChars { get; set; }
}
public class RandomTextGenerator : IRandomTextGenerator
{
/// <summary>
/// Initializes a new instance of the <see cref="RandomTextGenerator"/> class.
/// </summary>
public RandomTextGenerator()
{
Settings = new RandomTextGeneratorSettings();
Settings.Length = 5;
Settings.AllowedChars = "ABCDEFGHJKLMNPRSTUVWXY3456789";
}
#region IRandomTextGenerator Members
/// <summary>
/// Gets or sets the settings.
/// </summary>
/// <value>The settings.</value>
public RandomTextGeneratorSettings Settings { get; set; }
/// <summary>
/// Generate the random text.
/// </summary>
/// <returns></returns>
public string Generate()
{
Byte[] randomBytes = new Byte[Settings.Length];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(randomBytes);
char[] chars = new char[Settings.Length];
int allowedCharCount = Settings.AllowedChars.Length;
string allowedChars = Settings.AllowedChars;
for (int i = 0; i < Settings.Length; i++)
{
chars[i] = allowedChars[(int)randomBytes[i] % allowedCharCount];
}
return new string(chars);
}
#endregion
}
public class CaptchaImageActionResult : ActionResult
{
public string RandomWord { get; set; }
public override void ExecuteResult(ControllerContext context)
{
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap((int)Math.Ceiling((RandomWord.Length * 14.5)), 22);
Graphics graphics = Graphics.FromImage(bmp);
Random random = new Random();
graphics.Clear(Color.White);
for (int i = 0; i < 20; i++)
{
int x1 = random.Next(bmp.Width);
int x2 = random.Next(bmp.Width);
int y1 = random.Next(bmp.Height);
int y2 = random.Next(bmp.Height);
if (i / 2 == 0)
{
graphics.DrawLine(new Pen(Color.DarkCyan), x1, y1, x2, y2);
}
else
{
graphics.DrawLine(new Pen(Color.Chocolate), x1, y1, x2, y2);
}
graphics.Flush();
}
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, bmp.Width, bmp.Height), Color.Blue, Color.DarkRed, 1.2f, true);
graphics.DrawString(RandomWord, font, brush, 2, 2);
for (int i = 0; i < 5; i++)
{
int x = random.Next(bmp.Width);
int y = random.Next(bmp.Height);
bmp.SetPixel(x, y, Color.FromArgb(random.Next()));
}
graphics.DrawRectangle(new Pen(Color.Silver), 0, 0, bmp.Width - 1, bmp.Height - 1);
context.HttpContext.Response.ContentType = "image/gif";
bmp.Save(context.HttpContext.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
font.Dispose();
graphics.Dispose();
bmp.Dispose();
}
}
public CaptchaImageActionResult CaptchaImage()
{
var setting = new Framework.Utility.Captcha.RandomTextGenerator();
setting.Settings.Length = 4;
string strRandom = setting.Generate();
HttpContext.Session["RandomText"] = strRandom;
return new CaptchaImageActionResult() { RandomWord = strRandom };
}
验证码
<input value="" type="text" name="CaptchaImage" id="CaptchaImage" style="width: 60px;" />
<img style="cursor:pointer; vertical-align:middle" title="看不清楚请刷新!" id="valiCode" style="cursor: pointer;" src="/login/CaptchaImage" alt="" onclick="this.src='/login/CaptchaImage?dt='+ new Date().getTime()"/>
<span class="Types" style="color: Red">*</span>