注意:只是一个演示思想的代码,代码很乱
default.aspx
<form id="form1" runat="server">
<div runat="server" id="div1">
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:TextBox ID="TextBox1" runat="server" TextMode="Password"></asp:TextBox><br />
<table>
<tr>
<td width="40px">
<input type="button" ID="Button1" runat="server" style="30px"/></td><td width="40px">
<input type="button" ID="Button2" runat="server" style="30px"/></td><td width="40px">
<input type="button" ID="Button3" runat="server" style="30px"/></td>
</tr>
<tr>
<td width="40px"><input type="button" ID="Button4" runat="server" style="30px"/></td><td width="40px">
<input type="button" ID="Button5" runat="server" style="30px"/></td><td width="40px">
<input type="button" ID="Button6" runat="server" style="30px"/></td>
</tr>
<tr>
<td width="40px"><input type="button" ID="Button7" runat="server" style="30px"/></td><td width="40px">
<input type="button" ID="Button8" runat="server" style="30px"/></td><td width="40px">
<input type="button" ID="Button9" runat="server" style="30px"/></td>
</tr>
<tr>
<td width="40px"><input type="button" ID="Button10" runat="server" style="30px"/></td><td width="80px" colspan="2"><input type="button" value="重新输入" onclick="location.href=location.href" /></td>
</tr>
</table>
<asp:Button ID="Button12" runat="server" Text="确认" OnClick="Button12_Click" Width="127px" />
</div>
</form>
<div runat="server" id="div1">
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:TextBox ID="TextBox1" runat="server" TextMode="Password"></asp:TextBox><br />
<table>
<tr>
<td width="40px">
<input type="button" ID="Button1" runat="server" style="30px"/></td><td width="40px">
<input type="button" ID="Button2" runat="server" style="30px"/></td><td width="40px">
<input type="button" ID="Button3" runat="server" style="30px"/></td>
</tr>
<tr>
<td width="40px"><input type="button" ID="Button4" runat="server" style="30px"/></td><td width="40px">
<input type="button" ID="Button5" runat="server" style="30px"/></td><td width="40px">
<input type="button" ID="Button6" runat="server" style="30px"/></td>
</tr>
<tr>
<td width="40px"><input type="button" ID="Button7" runat="server" style="30px"/></td><td width="40px">
<input type="button" ID="Button8" runat="server" style="30px"/></td><td width="40px">
<input type="button" ID="Button9" runat="server" style="30px"/></td>
</tr>
<tr>
<td width="40px"><input type="button" ID="Button10" runat="server" style="30px"/></td><td width="80px" colspan="2"><input type="button" value="重新输入" onclick="location.href=location.href" /></td>
</tr>
</table>
<asp:Button ID="Button12" runat="server" Text="确认" OnClick="Button12_Click" Width="127px" />
</div>
</form>
default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
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 _Default : System.Web.UI.Page
{
private Random rnd = new Random();
private string GetOneRandomNum()
{
string strtemp;
int itmp = rnd.Next(36);
if (itmp < 10)
strtemp = rnd.Next(10).ToString();
else
strtemp = Convert.ToChar(rnd.Next(26) + 'A').ToString();
return strtemp;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["SecurityCode"] = "";
System.Collections.Generic.Dictionary<string, string> pswList = new System.Collections.Generic.Dictionary<string, string>();
string psw = Request.QueryString["psw"];
int strength = 10;
// 位置初始化
int left = 50 + rnd.Next(150);
int top = 50 + rnd.Next(150);
div1.Attributes.Add("style", "left:" + left + "px; top:" + top + "px; position:absolute;");
// 初始化密码的对应随机数
for (int i = 0; i < psw.Length; i++)
{
string tmp = "";
for (int j = 0; j < strength; j++)
{
tmp += GetOneRandomNum();
}
if (!pswList.ContainsKey(psw[i].ToString())) pswList.Add(psw[i].ToString(), tmp);
}
//安全码
for (int i = 0; i < psw.Length; i++)
{
Session["SecurityCode"] += pswList[psw[i].ToString()];
}
//为按钮随机排列并且分配安全码
System.Collections.Generic.List<int> list = new System.Collections.Generic.List<int>();
for (int i = 0; i <= 9; i++)
list.Add(i);
for (int i = 1; i <= 10; i++)
{
int num = list[rnd.Next(list.Count)];
list.Remove(num);
HtmlInputButton btn = Page.FindControl("Button" + i) as HtmlInputButton;
if (btn != null)
{
btn.Value = num.ToString();
if (pswList.ContainsKey(num.ToString()))
btn.Attributes.Add("onclick", "document.getElementById('HiddenField1').value+='" + pswList[num.ToString()] + "';document.getElementById('TextBox1').value+='1';");
else
{
string tmp = "";
for (int k = 0; k < strength; k++)
{
tmp += GetOneRandomNum();
}
btn.Attributes.Add("onclick", "document.getElementById('HiddenField1').value+='" + tmp + "';document.getElementById('TextBox1').value+='1';");
}
}
}
}
}
protected void Button12_Click(object sender, EventArgs e)
{
if (HiddenField1.Value != null && Session["SecurityCode"] != null)
{
if (HiddenField1.Value == Session["SecurityCode"].ToString())
{
Response.Write("<script>alert('正确');window.top.location.href='ok.aspx';</script>");
}
else
{
Response.Write("<script>alert('错误');location.href=location.href;</script>");
}
}
}
}
using System.Data;
using System.Configuration;
using System.Web;
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 _Default : System.Web.UI.Page
{
private Random rnd = new Random();
private string GetOneRandomNum()
{
string strtemp;
int itmp = rnd.Next(36);
if (itmp < 10)
strtemp = rnd.Next(10).ToString();
else
strtemp = Convert.ToChar(rnd.Next(26) + 'A').ToString();
return strtemp;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["SecurityCode"] = "";
System.Collections.Generic.Dictionary<string, string> pswList = new System.Collections.Generic.Dictionary<string, string>();
string psw = Request.QueryString["psw"];
int strength = 10;
// 位置初始化
int left = 50 + rnd.Next(150);
int top = 50 + rnd.Next(150);
div1.Attributes.Add("style", "left:" + left + "px; top:" + top + "px; position:absolute;");
// 初始化密码的对应随机数
for (int i = 0; i < psw.Length; i++)
{
string tmp = "";
for (int j = 0; j < strength; j++)
{
tmp += GetOneRandomNum();
}
if (!pswList.ContainsKey(psw[i].ToString())) pswList.Add(psw[i].ToString(), tmp);
}
//安全码
for (int i = 0; i < psw.Length; i++)
{
Session["SecurityCode"] += pswList[psw[i].ToString()];
}
//为按钮随机排列并且分配安全码
System.Collections.Generic.List<int> list = new System.Collections.Generic.List<int>();
for (int i = 0; i <= 9; i++)
list.Add(i);
for (int i = 1; i <= 10; i++)
{
int num = list[rnd.Next(list.Count)];
list.Remove(num);
HtmlInputButton btn = Page.FindControl("Button" + i) as HtmlInputButton;
if (btn != null)
{
btn.Value = num.ToString();
if (pswList.ContainsKey(num.ToString()))
btn.Attributes.Add("onclick", "document.getElementById('HiddenField1').value+='" + pswList[num.ToString()] + "';document.getElementById('TextBox1').value+='1';");
else
{
string tmp = "";
for (int k = 0; k < strength; k++)
{
tmp += GetOneRandomNum();
}
btn.Attributes.Add("onclick", "document.getElementById('HiddenField1').value+='" + tmp + "';document.getElementById('TextBox1').value+='1';");
}
}
}
}
}
protected void Button12_Click(object sender, EventArgs e)
{
if (HiddenField1.Value != null && Session["SecurityCode"] != null)
{
if (HiddenField1.Value == Session["SecurityCode"].ToString())
{
Response.Write("<script>alert('正确');window.top.location.href='ok.aspx';</script>");
}
else
{
Response.Write("<script>alert('错误');location.href=location.href;</script>");
}
}
}
}