using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace session验证码
{
public partial class WebForm1 : System.Web.UI.Page
{
//我们平常在在登陆网页的时候需要输的“入验证码”的原理
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Random read = new Random(); //
int i = read.Next(1000,10000); //产生一个四位的随机数(验证码)
Session["验证码"] = i; //将这个随机数保存到session中去
TextBox1.Text = Convert.ToString(Session["验证码"]);//将这个随机数用TextBox1控件显示出来(当然,这里只是讲随机数原理性的东西。实际的验证码是给他放到一副图片里去的)
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string yzm = Convert.ToString(Session["验证码"]); //取得session中的随机数(验证码)
if (TextBox2.Text == yzm) //如果随机数与TextBox2中输入的数据一致。那么就正确,否则错误
{
Label1.Text = "正确";
}
else
{
Label1.Text = "错误";
}
}
}
}原文地址:http://blog.csdn.net/fanbin168/article/details/38392363