复制代码 代码如下:
<%@ 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="确定" />
</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()
pri