Logon System of ASP. NET cainiao Road, asp.net bird road Logon
Background
I am an ASP. NET cainiao, now learning ASP. NET. I have recorded the code I personally typed here. There is not much reference value. Please see the friends who gave me a thumbs up to support me. Thank you.Website Introduction
A rough logon example is provided based on the example in the book. The code in the example is customized by yourself, and the encapsulation method is rarely used, it is to let yourself remember the process more clearly.
This website includes three functions: Registration, logon, and password change.
Registration Introduction
Create a Web form, that is, UserManagers. aspx. Do not paste the front-end code.Then write the registration method, including the user name as the primary key, SqlDataReader reading the database, SqlCommand parameter adding data, and other key points.
Protected void button#click (object sender, EventArgs e) {if (txtName. text = "" | txtPwd. text = "" | txtConfirm. text = "") {this. page. registerStartupScript ("ss", "<script> alert ('user name and password cannot be blank ') </script>"); return;} if (txtPwd. text. equals (txtConfirm. text) {// check whether the current user has SqlConnection sqlConn = new SqlConnection (ConfigurationManager. connectionStrings ["connection"]. connectionString); sqlConn. open (); String SQL = "select * from tb_user where username = '" + txtName. text. trim () + "'"; SqlCommand sqlCommand = new SqlCommand (SQL, sqlConn); SqlDataReader sqlDataReader = sqlCommand. executeReader (); if (sqlDataReader. read () {Page. registerStartupScript ("", "<script> alert ('user name already exists! ') </Script> "); return;} sqlDataReader. close (); // new user string strInsert = "insert into tb_user (username, pwd, marks) values (@ username, @ pwd, @ marks )"; sqlCommand = new SqlCommand (strInsert, sqlConn); sqlCommand. parameters. add ("@ username", SqlDbType. varChar); sqlCommand. parameters ["@ username"]. value = txtName. text; sqlCommand. parameters. add ("@ pwd", SqlDbType. varChar, 20); sqlCommand. parameters ["@ pwd "]. Value = txtPwd. text; sqlCommand. parameters. add ("@ marks", SqlDbType. varChar, 1000); sqlCommand. parameters ["@ marks"]. value = "zbq test"; sqlCommand. executeNonQuery (); sqlConn. close (); Page. registerStartupScript ("", "<script> alert ('registration successful! ') </Script> "); Response. Redirect (" Default. aspx? Name = "+ txtName. Text + "");}}
The interface effect is as follows:
Logon Introduction
First, add the logon window ManageLogin. aspx,Then write the logon code, including the verification code.
Protected void btnLogin_Click (object sender, EventArgs e) {if (string. isNullOrEmpty (txtName. text) | string. isNullOrEmpty (txtPwd. text) | string. isNullOrEmpty (txtValid. text) {Page. registerStartupScript ("", "<script> alert ('the information is incomplete! ') </Script> "); return;} if (! TxtValid. Text. ToUpper (). Equals (Session ["ValidNums"]) {Page. RegisterStartupScript ("", "<script> alert ('incorrect verification code! ') </Script> "); return;} SqlConnection SQL = new SqlConnection (ConfigurationManager. connectionStrings ["connection"]. connectionString); SQL. open (); string select = "select * from tb_user t where t. username = '"+ txtName. text. trim () + "'and pwd ='" + txtPwd. text. trim () + "'"; SqlCommand command = new SqlCommand (select, SQL); SqlDataReader dataReader = command. executeReader (); if (dataReader. read ()) {// Jump to Response. Redirect ("Default. aspx? Name = "+ txtName. Text +" ");} else {Page. RegisterStartupScript (" "," <script> alert ('account Name or Password error! ') </Script> "); dataReader. Close (); return ;}
Logon Effect
Password Change
First, create an EditPwd. aspx form.
<Table class = "table" border = "1px" align = "center"> <tr> <td class = "firstTd"> User Name: </td> <asp: dropDownList runat = "server" ID = "names" Width = "200px" Height = "20px"/> </td> </tr> <td class = "firstTd "> original password: </td> <asp: textBox runat = "server" ID = "txtOldPwd" TextMode = "Password"/> </td> </tr> <td class = "firstTd"> new Password: </td> <asp: TextBox runat = "server" ID = "txtNewPwd" TextMode = "Password"> </asp: textBox> </td> </tr> <td class = "firstTd"> & nbsp; </td> <td align = "right"> <span> <asp: button runat = "server" ID = "btnSure" OnClick = "btnSure_Click" Text = "Confirm Logon"/> <asp: button runat = "server" ID = "btnCancle" OnClick = "btnCancle_Click" Text = "cancel"/> </span> </td> </tr> </table>
Then write the modification method, including SqlDataAdapter + DataSet key points
Protected void Page_Load (object sender, EventArgs e) {// initialize the data if (! IsPostBack) {SqlConnection SQL = new SqlConnection (ConfigurationManager. connectionStrings ["connection"]. connectionString); SQL. open (); string select = "select * from tb_user"; SqlDataAdapter sqlDataAdapter = new SqlDataAdapter (select, SQL); DataSet dataSet = new DataSet (); sqlDataAdapter. fill (dataSet); SQL. close (); if (dataSet. tables [0]. rows. count> 0) {for (int index = 0; index <dataSet. tables [0 ]. Rows. count; index ++) {names. items. add (dataSet. tables [0]. rows [index] [1]. toString () ;}}} protected void btnSure_Click (object sender, EventArgs e) {if (string. isNullOrEmpty (txtNewPwd. text) | string. isNullOrEmpty (txtOldPwd. text) {Page. registerStartupScript ("", "<script> alert ('the password cannot be blank or cannot be equal! ') </Script> "); return;} SqlConnection sqlConnection = new SqlConnection (" server = PC-20150424DMHQ; database = MyDatas; uid = sa; pwd = 123456 "); string select = "select * from tb_user where username = '" + names. text + "'"; SqlCommand sqlCommand = new SqlCommand (select, sqlConnection); sqlConnection. open (); SqlDataReader sqlDataReader = sqlCommand. executeReader (); if (sqlDataReader. read () {if (sqlDataReader ["Pwd"]. ToString ()! = TxtOldPwd. Text) {Page. RegisterStartupScript ("", "<script> alert ('incorrect password! ') </Script> "); return ;}} else {Page. RegisterStartupScript (" "," <script> alert ('database connection error! ') </Script> "); return;} sqlConnection. close (); sqlDataReader. close (); // change the password sqlConnection. open (); string updatePwd = "update tb_user set pwd = '" + txtNewPwd. text + "'where username = '" + names. text + "'"; sqlCommand = new SqlCommand (updatePwd, sqlConnection); sqlCommand. executeNonQuery (); sqlConnection. close (); Page. registerStartupScript ("", "<script> alert ('modification successful! ') </Script> "); Page_Load (null, null );}
Password modification page