<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>無標題頁</title>
<script type="text/javascript">
function DoFresh() {
document.getElementById("Image1").src = "VerifyCode.aspx";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
驗證碼:<asp:TextBox ID="txtValidateCode" runat="server"></asp:TextBox>
</td>
<td>
<asp:Image ID="Image1" runat="server" />
<a href="javascript:DoFresh();">看不清?</a>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<br />
<asp:Literal ID="litErrorMsg" runat="server"></asp:Literal>
<asp:Button ID="btnSubmit" runat="server" Text="確定" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Image1.ImageUrl = "VerifyCode.aspx";
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Session["ValidateCode"] != null)
{
string outputValidateCode = Session["ValidateCode"] as string;
string inputValidateCode = txtValidateCode.Text.Trim();
if (string.Compare(outputValidateCode, inputValidateCode, true) != 0)
{
//Response.Write("<script>javascript:alert('輸入的驗證碼錯誤!');</script>");
litErrorMsg.Text = "輸入的驗證碼錯誤!";
}
else
{
//Response.Write("<script>javascript:alert('輸入的驗證碼正確!');</script>");
litErrorMsg.Text = "輸入的驗證碼正確!";
}
}
}
#region 調用下面的方法實現客戶端保存Cookie驗證模式
private void ValidateMethod()
{
if (Request.Cookies["CheckCode"] == null)
{
litErrorMsg.Text = "您的瀏覽器設置已被禁用 Cookies,您必須設置瀏覽器允許使用 Cookies 選項后才能使用本系統。";
litErrorMsg.Visible = true;
return;
}
if (String.Compare(Request.Cookies["CheckCode"].Value, TextBox1.Text.ToString().Trim(), true) != 0)
{
litErrorMsg.Text = "<font color=red>對不起,驗證碼錯誤!</font>";
litErrorMsg.Visible = true;
return;
}
else
{
litErrorMsg.Text = "<font color=green>恭喜,驗證碼輸入正確!</font>";
litErrorMsg.Visible = true;
}
}
#endregion
}
//VerifyCode.aspx為默認生成的代碼
代碼如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
public partial class VerifyCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//GenerateValidateCode();
GenerateVerifyImage(4);//GenerateVerifyImage(int length)
}#region 【無刷新仿google波形扭曲彩色】驗證碼樣式0___GenerateValidateCode()
private void GenerateValidateCode()
{
this.Length = this.length;
this.FontSize = this.fontSize;
this.Chaos = this.chaos;
this.BackgroundColor = this.backgroundColor;
this.ChaosColor = this.chaosColor;
this.CodeSerial = this.codeSerial;
this.Colors = this.colors;
this.Fonts = this.fonts;
this.Padding = this.padding;
string VNum = this.CreateVerifyCode(); //取隨機碼
Session["ValidateCode"] = VNum.ToUpper();//取得驗證碼,以便后來驗證
this.CreateImageOnPage(VNum, this.Context); //
// 1. 生成隨機背景顏色
int nRed, nGreen, nBlue; // 背景的三元色
System.Random rd = new Random((int)System.DateTime.Now.Ticks);
nRed = rd.Next(255) % 128 + 128;
nGreen = rd.Next(255) % 128 + 128;
nBlue = rd.Next(255) % 128 + 128;
// 2. 填充位圖背景
System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bmp);
graph.FillRectangle(new SolidBrush(System.Drawing.Color.FromArgb(nRed, nGreen, nBlue))
, 0
, 0
, nBmpWidth
, nBmpHeight);
// 3. 繪制干擾線條,采用比背景略深一些的顏色
int nLines = 3;
System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.FromArgb(nRed - 17, nGreen - 17, nBlue - 17), 2);
for (int a = 0; a < nLines; a++)
{
int x1 = rd.Next() % nBmpWidth;
int y1 = rd.Next() % nBmpHeight;
int x2 = rd.Next() % nBmpWidth;
int y2 = rd.Next() % nBmpHeight;
graph.DrawLine(pen, x1, y1, x2, y2);
}
// 采用的字符集,可以隨即拓展,并可以控制字符出現的幾率
string strCode = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
// 4. 循環取得字符,并繪制
for (int i = 0; i < nLen; i++)
{
int x = (i * 13 + rd.Next(3));
int y = rd.Next(4) + 1;
// 確定字體
System.Drawing.Font font = new System.Drawing.Font("Courier New",//文字字體類型
12 + rd.Next() % 4,//文字字體大小
System.Drawing.FontStyle.Bold);//文字字體樣式
char c = strCode[rd.Next(strCode.Length)]; // 隨機獲取字符
validateCode += c.ToString();
// 繪制字符
graph.DrawString(c.ToString(),
font,
new SolidBrush(System.Drawing.Color.FromArgb(nRed - 60 + y * 3, nGreen - 60 + y * 3, nBlue - 40 + y * 3)),
x,
y);
}
Session["ValidateCode"] = validateCode;
//對圖像進行彎曲
TwistImage(bmp, true, 4, 4);
Response.BufferOutput = true; //特別注意
Response.Cache.SetExpires(DateTime.Now.AddMilliseconds(-1));//特別注意
Response.Cache.SetCacheability(HttpCacheability.NoCache);//特別注意
Response.AppendHeader("Pragma", "No-Cache"); //特別注意
// 5. 輸出字節流
MemoryStream bstream = new MemoryStream();
bmp.Save(bstream, ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/JPEG";
Response.BinaryWrite(bstream.ToArray());
Response.End();
bstream.Close();
bstream = null;
bmp.Dispose();
bmp = null;
graph.Dispose();
}
///<summary>
///得到驗證碼圖片的寬度
///</summary>
///<paramname="validateNumLength">驗證碼的長度</param>
///<returns></returns>
public static int GetImagewidth(int validateNumLength)
{
return (int)(13 * validateNumLength + 5);
}
///<summary>
///得到驗證碼的高度
///</summary>
///<returns></returns>
public static int GetImageHeight()
{
return 25;
}
#endregion
}
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com