📄 webform1.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb ;
using System.Security.Cryptography ;
using System.Text;
namespace EncryptInfo
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtNameAdd;
protected System.Web.UI.WebControls.TextBox txtPwdAdd;
protected System.Web.UI.WebControls.Button btnAdd;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button btnCheck;
protected System.Web.UI.WebControls.TextBox txtNameCheck;
protected System.Web.UI.WebControls.TextBox txtPwdCheck;
protected System.Web.UI.WebControls.Label Label6;
protected System.Web.UI.WebControls.Label Label7;
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected System.Web.UI.WebControls.Label Label2;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
this.btnCheck.Click += new System.EventHandler(this.btnCheck_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnAdd_Click(object sender, System.EventArgs e)
{
if ((txtNameAdd.Text != "") && (txtPwdAdd.Text!=""))
{
//new a dbconnection,数据库为db目录下的userdb.mdb
string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ Server.MapPath(@".\db\userdb.mdb")
+ ";Mode=Share Deny None;Persist Security Info=False";
OleDbConnection conn = new OleDbConnection(connstr);
conn.Open ();
//检查是否用户名存在
string sql = "SELECT * FROM [userinfo] WHERE [user_name]='" + txtNameAdd.Text + "'" ;
OleDbCommand cmd = new OleDbCommand(sql,conn);
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read() == true)
{
MsgBox("用户名存在!");
return;
}
dr.Close ();
//使用MD5 Hash算法进行散列加密
MD5CryptoServiceProvider HashMD5 = new MD5CryptoServiceProvider();
string newPwd = ASCIIEncoding.ASCII.GetString (HashMD5.ComputeHash( ASCIIEncoding.ASCII.GetBytes(txtPwdAdd.Text )));
//将加密后的password添加如数据库中
sql = "INSERT INTO [userinfo]([user_name],[user_pwd])"
+ "VALUES('" + txtNameAdd.Text + "','" + newPwd + "')";
cmd.CommandText = sql;
cmd.ExecuteNonQuery ();
conn.Close();
MsgBox("成功添加用户:" + txtNameAdd.Text + " 密码:" + txtPwdAdd.Text );
}
}
private void MsgBox(string msg)
{
Response.Write ("<script language='JavaScript'>window.alert('" + msg + "');</script>");
}
private void btnCheck_Click(object sender, System.EventArgs e)
{
if ((txtNameCheck.Text != "") && (txtPwdCheck.Text!=""))
{
//new a dbconnection,数据库为db目录下的userdb.mdb
string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ Server.MapPath(@".\db\userdb.mdb")
+ ";Mode=Share Deny None;Persist Security Info=False";
OleDbConnection conn = new OleDbConnection(connstr);
conn.Open ();
//根据用户名,读出密码
string sql = "SELECT [user_pwd] FROM [userinfo] WHERE [user_name]='" + txtNameCheck.Text + "'";
OleDbCommand cmd = new OleDbCommand(sql,conn);
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read() == true)
{
//将输入密码进行散列加密,与读出密码进行比较
MD5CryptoServiceProvider HashMD5 = new MD5CryptoServiceProvider();
string newPwd = ASCIIEncoding.ASCII.GetString (HashMD5.ComputeHash( ASCIIEncoding.ASCII.GetBytes(txtPwdCheck.Text )));
if (dr.GetValue(0).ToString() == newPwd)
{
MsgBox("通过验证!");
}
else
{
MsgBox("用户信息错误!");
}
}
dr.Close ();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -