效果图(点击验证码可以更换):
验证码的实现思路是:用一个页面产生编码,并输出,然后用一个页面接受.
ValidateCode.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Drawing;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _validate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ //生成验证码
if (Session.Contents["validate"] == null)
Session.Add("validate", null);
Random rand = new Random();
string vaildate = rand.Next(1000, 10000).ToString();
Session.Contents["validate"] = vaildate;
System.Drawing.Image image = new System.Drawing.Bitmap(30, 16);
System.Drawing.Graphics formGraphice = System.Drawing.Graphics.FromImage(image);
System.Drawing.Font font = new System.Drawing.Font("Arial", 10, FontStyle.Regular);//字体与大小
System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
formGraphice.Clear(System.Drawing.Color.White);
System.Drawing.StringFormat format = new System.Drawing.StringFormat();
formGraphice.DrawString(vaildate, font, brush, 0, 00, format);
font.Dispose();
brush.Dispose();
formGraphice.Dispose();
image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
ValidateCode.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ValidateCode.aspx.cs" Inherits="ValidateCode" %>
<!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 runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="Image1" runat="server" Height="32px" Style="z-index: 100; left: 328px;
position: absolute; top: 176px" Width="80px" ImageUrl ="~/ValidateCode.aspx" />
</div>
</form>
</body>
</html>
Default页面接受验证码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
<link href="../App_Themes/Style.css" rel="stylesheet" type="text/css" />
<link href="../css/Css.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div style="width:569px; height:347px; border:green 1px solid; margin-top:80px;background-image:url('../images/admin.jpg')">
<div style="margin-top:100px; margin-left:160px;">
<div style="text-align:left;">用 户:<asp:TextBox ID="Text_Admin" runat="server" Width="130px"></asp:TextBox></div>
<div style="text-align:left;">密 码:<asp:TextBox ID="Text_PassWord" runat="server" TextMode="Password" Width="130px"></asp:TextBox></div>
<div style="text-align:left;">验证码:<asp:TextBox ID="Text_Validate" runat="server" MaxLength="4" Width="130px"></asp:TextBox><img alt="点击刷新" src="ValidateCode.aspx?time=' + Math.random();" style="cursor:pointer;" οnclick="this.src='validate.aspx?time=' + Math.random();" /></div>
<div style="margin-top:10px; margin-left:-180px;">
<asp:Button ID="button_Login" runat="server" Text="登 陆" Width="50px" Height="20px" CssClass="ButtonCss" /> <asp:Button ID="CanCel" runat="server" CssClass="ButtonCss" Text ="修改密码" Width="60px" /></div>
</div>
</form>
</body>
</html>