(十)用户管理模块(登录,注册,修改,重置密码MD5加密,时间戳+sql server 2014)
2018-04-02 19:10
916 查看
设计一个表,里面有3个字段,id varchar(16) primary key,psd varchar(16) not null, email varchar(30) not null.
邮箱这个字段是为了找回密码使用。
一开始我们的数据库里没有任何数据,所以我们是不能直接登录的,我们最开始先写注册。
上面的登录和注册界面中,我们都用到了验证控件。验证控件可以使我们减少代,在之后的几个页面中我们都会用到验证控件。在注册中,我们将邮箱的地址记录到了我们的数据库。当我们忘记密码的时候,就可以通过给邮箱发信息,重置密码。
在这之前,配置我们的web.config,以便连接数据库。<connectionStrings>
<add name="manageConnectionString" connectionString="Data Source=127.0.0.1;Initial Catalog=management;Integrated Security=True" providerName="System.Data.SqlClient" /></connectionStrings>
注册的后台代码enroll.aspx.cs
namespace manage
{
public partial class enroll : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
String str = ConfigurationManager.ConnectionStrings["manageConnectionString"].ConnectionString.ToString();
SqlConnection con = new SqlConnection(str);
con.Open();
SqlCommand comselect = new SqlCommand("select id form msg",con);
SqlDataReader rd = comselect.ExecuteReader();
if(rd.Read())
{
Response.Write("已存在该账号,请重新输入");
return;
}
rd.Close();
String insert="insert into mag values('"+TextBox1.Text.ToString()+"','"+TextBox2.Text.ToString()+"','"+TextBox3.Text.ToString()+"')";
SqlCommand cominsert=new SqlCommand(insert,con);
cominsert.ExecuteReader();
con.Close();
Response.Write("<script language=javascript>alert('注册成功')</script>");
Response.Redirect("~/enter.aspx");
}
}
}
出现了如下错误
1.在使用验证控件的当前页面后台page_load中加这样一句代码:UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;
2.修改<system.web> <compilation debug="true" targetFramework="4.0" /> </system.web>
在注册的时候,我们其实是可以将 密码加密保存到数据库中的,下面是使用了 md5加密的函数
public static string GetMD5(string str)
{
byte[] b = System.Text.Encoding.Default.GetBytes(str);
b = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(b);
string ret = " ";
for (int i = 0; i < b.Length; i++)
{
ret += b[i].ToString("x").PadLeft(2, '0');
}
return ret;
}
下面是登录
然后,我们利用控件将登陆界面拖好。这里的登录可以用上我们之前的验证码了。
设置一下imagebutton的属性,CausesValidation=False使它不激发验证事件。具体的验证代码我就不再写了,将我之前写的复制进来。先判断输入的验证码是否为空,然后在判断用户名是否存在,最后判断密码是否正确。下面是enter.aspx.cs代码
public partial class enter : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
if (TextBox3.Text.ToLower() == Session["code"].ToString())
{
String str = ConfigurationManager.ConnectionStrings["manageConnectionString"].ConnectionString.ToString();
SqlConnection con = new SqlConnection(str);
con.Open();
string select = "select *from msg where id='"+TextBox1.Text.ToString()+"'";
SqlDataAdapter da = new SqlDataAdapter(select, con);
DataSet add = new DataSet();
da.Fill(add);
if (add.Tables[0].Rows.Count > 0)
{
string strselect = "select * from msg where id='" + TextBox1.Text.Trim() + "' and psd='" + TextBox2.Text.Trim() + "'";
SqlCommand com=new SqlCommand(strselect,con);
SqlDataReader ad = com.ExecuteReader();
Session["id"] = TextBox1.Text;
if (ad.Read())
{
Response.Redirect("~/home.aspx");
}
else
Response.Write("<script language=javascript>alert('密码不正确')</script>");
}
else
Response.Write("用户名不存在");
}
else
Response.Write("<script language=javascript>alert('验证码不正确')</script>");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
}
在登录成功后我们在home这个界面需要使用session来判断id是否存在,这样子可以防止没有登录可以打开的情况,后面的页面都要用到这个。
修改密码首先要输入原始密码,原始密码正确才能进行修改。下面是修改密码的代码alterpasswordd.aspx
namespace manage
{
public partial class alterpassword : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["id"] != null)
Label1.Text = Session["id"].ToString();
else
Response.Redirect("~/enter.aspx");
}
protected void Button1_Click(object sender, EventArgs e)
{
String str = ConfigurationManager.ConnectionStrings["manageConnectionString"].ConnectionString.ToString();
SqlConnection con = new SqlConnection(str);
con.Open();
string select = "select *from msg where id='" +Session["id"].ToString() + "' and psd='" + TextBox3.Text.Trim() + "'";
SqlDataAdapter da = new SqlDataAdapter(select, con);
DataSet add = new DataSet();
da.Fill(add);
if (add.Tables[0].Rows.Count > 0)
{
if (TextBox2.Text.ToString().Length > 16)
Response.Write("密码不能超出16位");
else if(TextBox1.Text.Trim()==TextBox3.Text.Trim())
{
Response.Write("原始密码与新密码不能一样");
}
else
{
String update = "update msg set psd='" + TextBox1.Text.Trim() + "' where id=" + Session["id"];
SqlCommand cmd = new SqlCommand(update, con);
cmd.ExecuteNonQuery();
con.Close();
Session.Remove("id");
Response.Write("<script language='javascript'>alert('修改密码成功');window.location='enter.aspx'</script>");
}
}
else
Response.Write("当前密码错误");
}
}
} 找回密码首先需要一个界面,已获取账号和邮箱,通过邮箱进行密码的重置。在设置链接失效上,采用了时间戳。先获得发送邮箱时候的时间的时间戳,然后将时间戳变为时间,与当前时间做对比。
private DateTime StampToDateTime(string timeStamp)
{
DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(timeStamp + "0000000");
TimeSpan toNow = new TimeSpan(lTime);
return dateTimeStart.Add(toNow);
}//时间戳转时间
private int DateTimeToStamp(DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
return (int)(time - startTime).TotalSeconds;
} //时间转时间戳
找回密码的代码跟修改密码的代码很相似,这里不多写了。到这里,管理模块基本都完成了。
具体代码https://github.com/1126048156/manage.git
邮箱这个字段是为了找回密码使用。
一开始我们的数据库里没有任何数据,所以我们是不能直接登录的,我们最开始先写注册。
上面的登录和注册界面中,我们都用到了验证控件。验证控件可以使我们减少代,在之后的几个页面中我们都会用到验证控件。在注册中,我们将邮箱的地址记录到了我们的数据库。当我们忘记密码的时候,就可以通过给邮箱发信息,重置密码。
在这之前,配置我们的web.config,以便连接数据库。<connectionStrings>
<add name="manageConnectionString" connectionString="Data Source=127.0.0.1;Initial Catalog=management;Integrated Security=True" providerName="System.Data.SqlClient" /></connectionStrings>
注册的后台代码enroll.aspx.cs
namespace manage
{
public partial class enroll : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
String str = ConfigurationManager.ConnectionStrings["manageConnectionString"].ConnectionString.ToString();
SqlConnection con = new SqlConnection(str);
con.Open();
SqlCommand comselect = new SqlCommand("select id form msg",con);
SqlDataReader rd = comselect.ExecuteReader();
if(rd.Read())
{
Response.Write("已存在该账号,请重新输入");
return;
}
rd.Close();
String insert="insert into mag values('"+TextBox1.Text.ToString()+"','"+TextBox2.Text.ToString()+"','"+TextBox3.Text.ToString()+"')";
SqlCommand cominsert=new SqlCommand(insert,con);
cominsert.ExecuteReader();
con.Close();
Response.Write("<script language=javascript>alert('注册成功')</script>");
Response.Redirect("~/enter.aspx");
}
}
}
出现了如下错误
WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping。请添加一个名为 jquery (区分大小写)的 ScriptResourceMapping。
这个错误是我们使用了验证控件报的错。我们有如下方式解决这个问题1.在使用验证控件的当前页面后台page_load中加这样一句代码:UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;
2.修改<system.web> <compilation debug="true" targetFramework="4.0" /> </system.web>
在注册的时候,我们其实是可以将 密码加密保存到数据库中的,下面是使用了 md5加密的函数
public static string GetMD5(string str)
{
byte[] b = System.Text.Encoding.Default.GetBytes(str);
b = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(b);
string ret = " ";
for (int i = 0; i < b.Length; i++)
{
ret += b[i].ToString("x").PadLeft(2, '0');
}
return ret;
}
下面是登录
然后,我们利用控件将登陆界面拖好。这里的登录可以用上我们之前的验证码了。
设置一下imagebutton的属性,CausesValidation=False使它不激发验证事件。具体的验证代码我就不再写了,将我之前写的复制进来。先判断输入的验证码是否为空,然后在判断用户名是否存在,最后判断密码是否正确。下面是enter.aspx.cs代码
namespace manage
{public partial class enter : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
if (TextBox3.Text.ToLower() == Session["code"].ToString())
{
String str = ConfigurationManager.ConnectionStrings["manageConnectionString"].ConnectionString.ToString();
SqlConnection con = new SqlConnection(str);
con.Open();
string select = "select *from msg where id='"+TextBox1.Text.ToString()+"'";
SqlDataAdapter da = new SqlDataAdapter(select, con);
DataSet add = new DataSet();
da.Fill(add);
if (add.Tables[0].Rows.Count > 0)
{
string strselect = "select * from msg where id='" + TextBox1.Text.Trim() + "' and psd='" + TextBox2.Text.Trim() + "'";
SqlCommand com=new SqlCommand(strselect,con);
SqlDataReader ad = com.ExecuteReader();
Session["id"] = TextBox1.Text;
if (ad.Read())
{
Response.Redirect("~/home.aspx");
}
else
Response.Write("<script language=javascript>alert('密码不正确')</script>");
}
else
Response.Write("用户名不存在");
}
else
Response.Write("<script language=javascript>alert('验证码不正确')</script>");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
}
在登录成功后我们在home这个界面需要使用session来判断id是否存在,这样子可以防止没有登录可以打开的情况,后面的页面都要用到这个。
修改密码首先要输入原始密码,原始密码正确才能进行修改。下面是修改密码的代码alterpasswordd.aspx
namespace manage
{
public partial class alterpassword : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["id"] != null)
Label1.Text = Session["id"].ToString();
else
Response.Redirect("~/enter.aspx");
}
protected void Button1_Click(object sender, EventArgs e)
{
String str = ConfigurationManager.ConnectionStrings["manageConnectionString"].ConnectionString.ToString();
SqlConnection con = new SqlConnection(str);
con.Open();
string select = "select *from msg where id='" +Session["id"].ToString() + "' and psd='" + TextBox3.Text.Trim() + "'";
SqlDataAdapter da = new SqlDataAdapter(select, con);
DataSet add = new DataSet();
da.Fill(add);
if (add.Tables[0].Rows.Count > 0)
{
if (TextBox2.Text.ToString().Length > 16)
Response.Write("密码不能超出16位");
else if(TextBox1.Text.Trim()==TextBox3.Text.Trim())
{
Response.Write("原始密码与新密码不能一样");
}
else
{
String update = "update msg set psd='" + TextBox1.Text.Trim() + "' where id=" + Session["id"];
SqlCommand cmd = new SqlCommand(update, con);
cmd.ExecuteNonQuery();
con.Close();
Session.Remove("id");
Response.Write("<script language='javascript'>alert('修改密码成功');window.location='enter.aspx'</script>");
}
}
else
Response.Write("当前密码错误");
}
}
} 找回密码首先需要一个界面,已获取账号和邮箱,通过邮箱进行密码的重置。在设置链接失效上,采用了时间戳。先获得发送邮箱时候的时间的时间戳,然后将时间戳变为时间,与当前时间做对比。
private DateTime StampToDateTime(string timeStamp)
{
DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(timeStamp + "0000000");
TimeSpan toNow = new TimeSpan(lTime);
return dateTimeStart.Add(toNow);
}//时间戳转时间
private int DateTimeToStamp(DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
return (int)(time - startTime).TotalSeconds;
} //时间转时间戳
找回密码的代码跟修改密码的代码很相似,这里不多写了。到这里,管理模块基本都完成了。
具体代码https://github.com/1126048156/manage.git
相关文章推荐
- 购物网站20:用户/顾客action----用户列表---用户退出---用户管理---用户登录---用户注册---修改密码---发送邮件
- Android基于XMPP Smack Openfire下学习开发IM(一)实现用户注册、登录、修改密码和注销等
- Flask -- 使用数据库(Sqlite3)、用户注册、登录注销、修改密码
- Android基于XMPP Smack Openfire下学习开发IM(一)实现用户注册、登录、修改密码和注销等
- openfire Android学习(一)----实现用户注册、登录、修改密码和注销等
- RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2->用户管理模块新增“重置用户密码”功能
- openfire Android学习(一)----实现用户注册、登录、修改密码和注销等
- (01) 需求征集 -- 用户注册、登录、修改密码、设置密码等方面的需求征集
- Android基于XMPP Smack Openfire下学习开发IM(一)实现用户注册、登录、修改密码和注销等
- Android基于XMPP Smack Openfire下学习开发IM(一)实现用户注册、登录、修改密码和注销等
- Trac--用户管理(安装用户注册及登录插件模块)
- 【Struts2+Hibernate3+Spring3】利用SSH整合,完成打印用户表,用户登录、注册、修改密码系统
- shiro 用户权限管理(2)-----注册md5加密,登录验证
- [SqlServer]用户注册、登录、修改密码存储过程实例
- android注册 登录 修改帐号密码 添加资料 给新注册用户充值DEMO
- Oracle中管理用户(创建用户,用户加锁,用户解锁,修改用户密码,授权登录权限,撤销登录权限,授权连接权限,conn命令,创建角色,并为角色赋权限,将角色赋给指定用户)
- 【Openfire】网页版的用户注册、登录、修改密码
- Android基于XMPP Smack Openfire下学习开发IM(一)实现用户注册、登录、修改密码和注销等
- [SqlServer]用户注册、登录、修改密码存储过程实例
- RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2->用户管理模块新增“重置用户密码”功能