数据库名是Member
login.aspx代码:
<%@ 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 runat="server">
<title></title>
<style type="text/css">
.a
{
color: #FF0000;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="tb_login" style="text-align: center"><tr><td>用户名:</td><td>
<asp:TextBox ID="txtusername" runat="server"></asp:TextBox></td><td>
<asp:RequiredFieldValidator ID="ReqFV_username" runat="server"
ErrorMessage="*" ControlToValidate="txtusername"></asp:RequiredFieldValidator>
</td></tr>
<tr><td>密码:</td><td>
<asp:TextBox ID="txtpass" runat="server" TextMode="Password"></asp:TextBox></td><td><a href="passchange.aspx" class="a">忘记密码</a></td></tr>
<tr><td>验证码:</td><td>
<asp:TextBox ID="txtcode" runat="server"></asp:TextBox></td><td>
<asp:RequiredFieldValidator ID="ReqFV_code" runat="server"
ErrorMessage="*" ControlToValidate="txtcode"></asp:RequiredFieldValidator>
<img style=" cursor:hand;width: 76px; height: 21px" id="imgcode" src="checkcode.aspx" alt="看不清,请点击我!" οnclick="this.src=this.src+'?'" /></td></tr>
<tr><td>
<asp:Button ID="btnlogin" runat="server" Text="登录" οnclick="btnlogin_Click" /></td><td></td><td>
<asp:Label ID="lablogin" runat="server" Text="lablogin"></asp:Label></td></tr>
</table>
</div>
</form>
</body>
</html>
<!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 runat="server">
<title></title>
<style type="text/css">
.a
{
color: #FF0000;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="tb_login" style="text-align: center"><tr><td>用户名:</td><td>
<asp:TextBox ID="txtusername" runat="server"></asp:TextBox></td><td>
<asp:RequiredFieldValidator ID="ReqFV_username" runat="server"
ErrorMessage="*" ControlToValidate="txtusername"></asp:RequiredFieldValidator>
</td></tr>
<tr><td>密码:</td><td>
<asp:TextBox ID="txtpass" runat="server" TextMode="Password"></asp:TextBox></td><td><a href="passchange.aspx" class="a">忘记密码</a></td></tr>
<tr><td>验证码:</td><td>
<asp:TextBox ID="txtcode" runat="server"></asp:TextBox></td><td>
<asp:RequiredFieldValidator ID="ReqFV_code" runat="server"
ErrorMessage="*" ControlToValidate="txtcode"></asp:RequiredFieldValidator>
<img style=" cursor:hand;width: 76px; height: 21px" id="imgcode" src="checkcode.aspx" alt="看不清,请点击我!" οnclick="this.src=this.src+'?'" /></td></tr>
<tr><td>
<asp:Button ID="btnlogin" runat="server" Text="登录" οnclick="btnlogin_Click" /></td><td></td><td>
<asp:Label ID="lablogin" runat="server" Text="lablogin"></asp:Label></td></tr>
</table>
</div>
</form>
</body>
</html>
aspx.cs代码:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnlogin_Click(object sender, EventArgs e)
{
// //try
// //{
// SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=Member;Integrated Security=True");
// con.Open();
// string pass = FormsAuthentication.HashPasswordForStoringInConfigFile(txtpass.Text, "MD5");
// string sqlSel = "select count(*) from MemberInfo where userName=@name and userPass=@pass";
// SqlCommand com = new SqlCommand(sqlSel, con);
// com.Parameters.Add(new SqlParameter("name", SqlDbType.NVarChar, 20));
// com.Parameters["name"].Value = txtusername;
// com.Parameters.Add(new SqlParameter("pass", SqlDbType.NVarChar, 50));
// com.Parameters["pass"].Value = txtpass;
// if ( Convert.ToInt32( com.ExecuteScalar())>0)
// {
// lablogin.Text = "登陆成功";
// txtpass.Text = "";
// }
// else
// {
// lablogin.Text = "登录失败";
// }
// }
// //catch(Exception ex)
// //{
// // lablogin.Text = "" + ex.Message;
// //}
if (Request.Cookies["checkcode"].Value == txtcode.Text)
{
string sqlconnstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(sqlconnstr);
SqlCommand com = new SqlCommand();
com.Connection = con;
con.Open();
com.CommandText = "select * from MemberInfo where userName=@name and userPass=@pass";
com.Parameters.AddWithValue("@name", txtusername.Text);
com.Parameters.AddWithValue("@pass", txtpass.Text);
SqlDataReader sqldatareader = com.ExecuteReader();
if (sqldatareader.Read())
{
txtpass.Text = "";
txtcode.Text = "";
lablogin.Text = "登陆成功";
com = null;
con.Close();
con = null;
}
else
{
txtpass.Text = "";
txtcode.Text = "";
lablogin.Text = "用户名或密码错误";
com = null;
con.Close();
con = null;
}
}
else
{
txtpass.Text = "";
txtcode.Text = "";
lablogin.Text = "验证码错误";
}
}
}
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnlogin_Click(object sender, EventArgs e)
{
// //try
// //{
// SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=Member;Integrated Security=True");
// con.Open();
// string pass = FormsAuthentication.HashPasswordForStoringInConfigFile(txtpass.Text, "MD5");
// string sqlSel = "select count(*) from MemberInfo where userName=@name and userPass=@pass";
// SqlCommand com = new SqlCommand(sqlSel, con);
// com.Parameters.Add(new SqlParameter("name", SqlDbType.NVarChar, 20));
// com.Parameters["name"].Value = txtusername;
// com.Parameters.Add(new SqlParameter("pass", SqlDbType.NVarChar, 50));
// com.Parameters["pass"].Value = txtpass;
// if ( Convert.ToInt32( com.ExecuteScalar())>0)
// {
// lablogin.Text = "登陆成功";
// txtpass.Text = "";
// }
// else
// {
// lablogin.Text = "登录失败";
// }
// }
// //catch(Exception ex)
// //{
// // lablogin.Text = "" + ex.Message;
// //}
if (Request.Cookies["checkcode"].Value == txtcode.Text)
{
string sqlconnstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(sqlconnstr);
SqlCommand com = new SqlCommand();
com.Connection = con;
con.Open();
com.CommandText = "select * from MemberInfo where userName=@name and userPass=@pass";
com.Parameters.AddWithValue("@name", txtusername.Text);
com.Parameters.AddWithValue("@pass", txtpass.Text);
SqlDataReader sqldatareader = com.ExecuteReader();
if (sqldatareader.Read())
{
txtpass.Text = "";
txtcode.Text = "";
lablogin.Text = "登陆成功";
com = null;
con.Close();
con = null;
}
else
{
txtpass.Text = "";
txtcode.Text = "";
lablogin.Text = "用户名或密码错误";
com = null;
con.Close();
con = null;
}
}
else
{
txtpass.Text = "";
txtcode.Text = "";
lablogin.Text = "验证码错误";
}
}
}
zhuce.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="zhuce.aspx.cs" Inherits="zhuce" %>
<!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 runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 40px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr><td>用户名:</td>
<td>
<asp:TextBox ID="txtusername" runat="server"></asp:TextBox>
</td><td class="style1">
<asp:RequiredFieldValidator ID="ReqFV_username" runat="server"
ControlToValidate="txtusername" ErrorMessage="*"></asp:RequiredFieldValidator>
</td>
</tr>
<tr><td>密码:</td>
<td>
<asp:TextBox ID="txtuserpass" runat="server" TextMode="Password"></asp:TextBox>
</td><td class="style1">
<asp:RequiredFieldValidator ID="ReqFV_userpass" runat="server"
ControlToValidate="txtuserpass" ErrorMessage="*"></asp:RequiredFieldValidator>
</td></tr>
<tr><td>性别:</td><td>
<asp:DropDownList ID="DropDownList_sex" runat="server">
<asp:ListItem>男</asp:ListItem>
<asp:ListItem>女</asp:ListItem>
</asp:DropDownList>
</td><td class="style1"></td></tr>
<tr><td>年龄:</td><td>
<asp:TextBox ID="txtage" runat="server"></asp:TextBox></td><td class="style1">
</td></tr>
<tr><td>联系方式:</td><td>
<asp:TextBox ID="txttelnum" runat="server"></asp:TextBox></td>
<td class="style1">
</td></tr>
<tr><td>照片:</td><td>
<asp:FileUpload ID="FileUpload_photo" runat="server" /></td><td class="style1"></td></tr>
<tr><td>所在城市:</td><td>
<asp:DropDownList ID="DropDownList_city" runat="server">
<asp:ListItem>请选择所在城市</asp:ListItem>
<asp:ListItem>北京</asp:ListItem>
<asp:ListItem>上海</asp:ListItem>
<asp:ListItem>广州</asp:ListItem>
<asp:ListItem>深圳</asp:ListItem>
</asp:DropDownList>
</td><td class="style1"></td></tr>
</table>
<br />
</div>
<div><table>
<tr><td>
<asp:Button ID="btn_zhuce" runat="server" Text="注册" οnclick="btn_zhuce_Click" /></td><td>
<asp:Button ID="btn_login" runat="server" Text="返回登录"
οnclick="btn_login_Click" CausesValidation="False" /></td><td class="style1"> <asp:Label ID="labMes" runat="server" Text="Label"></asp:Label></td></tr>
</table></div>
</form>
</body>
</html>
<!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 runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 40px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr><td>用户名:</td>
<td>
<asp:TextBox ID="txtusername" runat="server"></asp:TextBox>
</td><td class="style1">
<asp:RequiredFieldValidator ID="ReqFV_username" runat="server"
ControlToValidate="txtusername" ErrorMessage="*"></asp:RequiredFieldValidator>
</td>
</tr>
<tr><td>密码:</td>
<td>
<asp:TextBox ID="txtuserpass" runat="server" TextMode="Password"></asp:TextBox>
</td><td class="style1">
<asp:RequiredFieldValidator ID="ReqFV_userpass" runat="server"
ControlToValidate="txtuserpass" ErrorMessage="*"></asp:RequiredFieldValidator>
</td></tr>
<tr><td>性别:</td><td>
<asp:DropDownList ID="DropDownList_sex" runat="server">
<asp:ListItem>男</asp:ListItem>
<asp:ListItem>女</asp:ListItem>
</asp:DropDownList>
</td><td class="style1"></td></tr>
<tr><td>年龄:</td><td>
<asp:TextBox ID="txtage" runat="server"></asp:TextBox></td><td class="style1">
</td></tr>
<tr><td>联系方式:</td><td>
<asp:TextBox ID="txttelnum" runat="server"></asp:TextBox></td>
<td class="style1">
</td></tr>
<tr><td>照片:</td><td>
<asp:FileUpload ID="FileUpload_photo" runat="server" /></td><td class="style1"></td></tr>
<tr><td>所在城市:</td><td>
<asp:DropDownList ID="DropDownList_city" runat="server">
<asp:ListItem>请选择所在城市</asp:ListItem>
<asp:ListItem>北京</asp:ListItem>
<asp:ListItem>上海</asp:ListItem>
<asp:ListItem>广州</asp:ListItem>
<asp:ListItem>深圳</asp:ListItem>
</asp:DropDownList>
</td><td class="style1"></td></tr>
</table>
<br />
</div>
<div><table>
<tr><td>
<asp:Button ID="btn_zhuce" runat="server" Text="注册" οnclick="btn_zhuce_Click" /></td><td>
<asp:Button ID="btn_login" runat="server" Text="返回登录"
οnclick="btn_login_Click" CausesValidation="False" /></td><td class="style1"> <asp:Label ID="labMes" runat="server" Text="Label"></asp:Label></td></tr>
</table></div>
</form>
</body>
</html>
zhuce.aspx.cs代码:
using System;
using System.Data.SqlClient;
using System.Configuration;
using System.Security.Cryptography;
using System.Web.Security;
public partial class zhuce : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private string Encrypt(string strPwd)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();//创建MD5对象
byte[] data = System.Text.Encoding.Default.GetBytes(strPwd);//将字符编码为一个字节序列
byte[] md5data = md5.ComputeHash(data);//计算data字节数组的哈希值
md5.Clear();//清空MD5对象
string str = "";//定义一个变量,用来记录加密后的密码
for (int i = 0; i < md5data.Length - 1; i++)//遍历字节数组
{
str += md5data[i].ToString("x").PadLeft(2, '0');//对遍历到的字节进行加密
}
return str;//返回得到的加密字符串
}
//private string GetMD5(string sDataIn)
//{
// MD5CryptoServiceProvider md5=new MD5CryptoServiceProvider();
// byte[]bytValue,bytHash;
// bytValue = System.Text.Encoding.UTF8.GetBytes(sDataIn);
// bytHash = md5.ComputeHash(bytValue);
// md5.Clear();
// string sTemp= " ";
// for(int i=0;i <bytHash.Length;i++)
// {
// sTemp+=bytHash[i].ToString( "X ").PadLeft(2, '0');
// }
// return sTemp.ToLower();
// }
protected void btn_zhuce_Click(object sender, EventArgs e)
{
string pass = FormsAuthentication.HashPasswordForStoringInConfigFile(txtuserpass.Text, "MD5");
string sqlconnstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(sqlconnstr);
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = @"insert into MemberInfo(userName,userPass,sex,age,telnum,photo,city)
values(@userName,@userPass,@sex,@age,@telnum,@photo,@city)";
com.Parameters.AddWithValue("@userName",txtusername.Text);
com.Parameters.AddWithValue("@userPass", txtuserpass.Text);
com.Parameters.AddWithValue("@sex",DropDownList_sex.Text);
com.Parameters.AddWithValue("@age",txtage.Text);
com.Parameters.AddWithValue("@telnum", txttelnum.Text);
com.Parameters.AddWithValue("@photo",FileUpload_photo.FileName);
com.Parameters.AddWithValue("@city", DropDownList_city.Text);
try
{
con.Open();
com.ExecuteNonQuery();
if (FileUpload_photo.HasFile == true)
{
FileUpload_photo.SaveAs(Server.MapPath(("./MPhotos/") + FileUpload_photo.FileName));
}
labMes.Text = "注册成功!";
}
catch (Exception ex)
{
labMes.Text="" + ex.Message;
}
}
protected void btn_login_Click(object sender, EventArgs e)
{
Response.Redirect("login.aspx");
}
}
using System.Data.SqlClient;
using System.Configuration;
using System.Security.Cryptography;
using System.Web.Security;
public partial class zhuce : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private string Encrypt(string strPwd)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();//创建MD5对象
byte[] data = System.Text.Encoding.Default.GetBytes(strPwd);//将字符编码为一个字节序列
byte[] md5data = md5.ComputeHash(data);//计算data字节数组的哈希值
md5.Clear();//清空MD5对象
string str = "";//定义一个变量,用来记录加密后的密码
for (int i = 0; i < md5data.Length - 1; i++)//遍历字节数组
{
str += md5data[i].ToString("x").PadLeft(2, '0');//对遍历到的字节进行加密
}
return str;//返回得到的加密字符串
}
//private string GetMD5(string sDataIn)
//{
// MD5CryptoServiceProvider md5=new MD5CryptoServiceProvider();
// byte[]bytValue,bytHash;
// bytValue = System.Text.Encoding.UTF8.GetBytes(sDataIn);
// bytHash = md5.ComputeHash(bytValue);
// md5.Clear();
// string sTemp= " ";
// for(int i=0;i <bytHash.Length;i++)
// {
// sTemp+=bytHash[i].ToString( "X ").PadLeft(2, '0');
// }
// return sTemp.ToLower();
// }
protected void btn_zhuce_Click(object sender, EventArgs e)
{
string pass = FormsAuthentication.HashPasswordForStoringInConfigFile(txtuserpass.Text, "MD5");
string sqlconnstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(sqlconnstr);
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = @"insert into MemberInfo(userName,userPass,sex,age,telnum,photo,city)
values(@userName,@userPass,@sex,@age,@telnum,@photo,@city)";
com.Parameters.AddWithValue("@userName",txtusername.Text);
com.Parameters.AddWithValue("@userPass", txtuserpass.Text);
com.Parameters.AddWithValue("@sex",DropDownList_sex.Text);
com.Parameters.AddWithValue("@age",txtage.Text);
com.Parameters.AddWithValue("@telnum", txttelnum.Text);
com.Parameters.AddWithValue("@photo",FileUpload_photo.FileName);
com.Parameters.AddWithValue("@city", DropDownList_city.Text);
try
{
con.Open();
com.ExecuteNonQuery();
if (FileUpload_photo.HasFile == true)
{
FileUpload_photo.SaveAs(Server.MapPath(("./MPhotos/") + FileUpload_photo.FileName));
}
labMes.Text = "注册成功!";
}
catch (Exception ex)
{
labMes.Text="" + ex.Message;
}
}
protected void btn_login_Click(object sender, EventArgs e)
{
Response.Redirect("login.aspx");
}
}