前台:
<div>
<asp:Label ID="Label1" runat="server" Text="账号:"></asp:Label>
<asp:TextBox ID="txtuser" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="密码:"></asp:Label>
<asp:TextBox ID="txtpass" runat="server"></asp:TextBox>
<br />
<br />
验证码:<img src="YanZheng.aspx" id="YanZheng" alt="" style="cursor:hand" hspace="0" align="top" />
<a href="javascript:document.getElementById('YanZheng').src='YanZheng.aspx?'+new Date;void(0);">换一张?</a>
<br />
输入验证码<asp:TextBox ID="txtYanZheng" runat="server"></asp:TextBox><br />
<asp:Button ID="btnlg" runat="server" Text="Button" οnclick="btnlg_Click" />
<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:Label ID="Label3" runat="server" Text=""></asp:Label>
<asp:Label ID="Label4" runat="server" Text=""></asp:Label>
</div>
后台:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Cookies["UserName"]!=null)
{
this.txtuser.Text = Request.Cookies["UserName"].Value;
}
if (Request.Cookies["UserName"]!=null&&Request.Cookies["Password"]!=null)
{
this.txtuser.Text = Request.Cookies["UserName"].Value;
this.txtpass.Text = Request.Cookies["Password"].Value;
}
}
}
protected void btnlg_Click(object sender, EventArgs e)
{
DataLoad();
data();
}
private void DataLoad()
{
if (txtYanZheng.Text.Equals(Session["CheckCode"]+""))
{
string contor = ConfigurationManager.ConnectionStrings["stcron"].ConnectionString;
SqlConnection coon = new SqlConnection(contor);
coon.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = coon;
cmd.CommandText = "select UserName,Password,UserId from T_User where UserName=@username ";
cmd.Parameters.AddWithValue("@username", txtuser.Text);
SqlDataAdapter adta = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adta.Fill(dt);
if (dt.Rows.Count>0)
{
string username = dt.Rows[0]["UserName"].ToString();
string pass = dt.Rows[0]["Password"].ToString();
if (txtuser.Text==username&&txtpass.Text==pass)
{
string userid = dt.Rows[0]["UserId"].ToString();
Session["Id"] = userid;
Session["name"] = username;
Response.Redirect("接受.aspx");
}
else if (txtuser.Text==username&&txtpass.Text!=pass)
{
Label3.Text = "密码错误";
}
}
else
{
Label3.Text = "用户不存在";
}
}
else
{
Label4.Text = "验证码不对";
}
}
private void data()//记住我
{
Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(-1);
Response.Cookies["Password"].Expires = DateTime.Now.AddDays(-1);
if (CheckBox1.Checked)
{
Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(7);
Response.Cookies["Password"].Expires = DateTime.Now.AddDays(7);
}
Response.Cookies["UserName"].Value = this.txtuser.Text.Trim();
Response.Cookies["Password"].Value = this.txtpass.Text.Trim();
}