功能有:非空验证、记住我功能、验证码
记住我功能的["username"]和["pass"]这两个字段必须与数据库中的字段一样
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Text;
using System.Security.Cryptography;
using System.Web.Security;
namespace lastOA
{
public partial class denglu : System.Web.UI.Page
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//记住我
if (Request.Cookies["username"] != null)
{
this.txtusername.Text = Request.Cookies["username"].Value;
}
if (Request.Cookies["username"] != null && Request.Cookies["pass"] != null)
{
this.txtusername.Text = Request.Cookies["username"].Value;
string xx = Request.Cookies["pass"].Value;
txtpassword.Text = xx;
}
//记住我Load事件代码结束
}
}
protected void ibdenglu_Click(object sender, ImageClickEventArgs e)
{
if (this.txtusername.Text == null && this.txtpassword.Text == null && this.txtyzm.Text == null)
{
lbcuowu.Text = "用户名、密码、验证码不能为空";
}
else
{
if (this.txtyzm.Text == Session["CheckCode"].ToString())//如果验证码校验通过则继续
{
using (SqlConnection conn = new SqlConnection(constr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from T_User where username=@username";
cmd.Parameters.AddWithValue("@username", txtusername.Text.Trim());
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
string username = reader.GetString(reader.GetOrdinal("username"));
string pass = reader.GetString(reader.GetOrdinal("pass"));
if (username == txtusername.Text.Trim())
{
if (pass == txtpassword.Text.Trim())
{
string leiname= Getleiname(txtusername.Text);
string name = Getname(txtusername.Text.Trim());
//Response.Redirect("left.aspx?leiname="+leiname);
Application["name"] = name;
Application["leiname"] = leiname;
date();//调用记住我方法
Response.Redirect("Index.aspx");
//Response.Write("<script>alert('"+aa+"')</script>");
}
else
{
lbcuowu.Text = "密码错误!";
}
}
}
else
{
lbcuowu.Text = "帐号错误";
}
}
}
}
}
else
{
//lbcuowu.Focus();
lbcuowu.Text = "验证码输入有误!";
}
}
}
private string Getname(string uname)
{
SqlConnection conn = new SqlConnection(constr);
SqlCommand cmd = conn.CreateCommand();
conn.Open();
cmd.CommandText = "select name from T_User where username=@uname";
cmd.Parameters.AddWithValue("@uname", uname);
string name = cmd.ExecuteScalar().ToString();
return name;
}
private string Getleiname(string uname)
{
SqlConnection conn = new SqlConnection(constr);
SqlCommand cmd = conn.CreateCommand();
conn.Open();
cmd.CommandText = "select leiname from T_User where username=@uname";
cmd.Parameters.AddWithValue("@uname",uname);
string leiname = cmd.ExecuteScalar().ToString();
return leiname;
}
//判断结束
//记住我方法
private void date()
{
Response.Cookies["username"].Expires = DateTime.Now.AddDays(-1);
Response.Cookies["pass"].Expires = DateTime.Now.AddDays(-1);
if (CheckBox1.Checked)
{
Response.Cookies["username"].Expires = DateTime.Now.AddDays(7);
Response.Cookies["pas"].Expires = DateTime.Now.AddDays(7);
}
Response.Cookies["username"].Value = this.txtusername.Text.Trim();
Response.Cookies["pass"].Value = this.txtpassword.Text.Trim();
}
}
}