·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> # 会员注册与登录模块

# 会员注册与登录模块

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

# 会员注册与登录模块

首先分别新建三个网页,register.aspx,login.aspx, yzm.aspx分别用于用户的注册,登录以及验证码的生成。

register.aspx前台代码:

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="register.aspx.cs" Inherits="_Default" %> 2  3 <!DOCTYPE html> 4  5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 8     <title>注册页面</title> 9     <style type ="text/CSS">10         .wrap {11             padding-top:40px;12             margin:0 auto;13             width:760px;14             height:426px;15         }16         .auto-style1 {17             width: 399px;18         }19     </style>20 </head>21 <body>22     <form id="form1" runat="server">23         <table>24             <tr>25                 <td>用户名:</td><td class="auto-style1"><asp:TextBox ID ="txtName"  runat ="server" AutoPostBack="true" OnTextChanged ="txtName_TextChanged"></asp:TextBox><asp:Label ID ="labuser" runat ="server"></asp:Label></td>26             </tr>27             <tr>28                 <td>密码:</td><td class="auto-style1"><asp:TextBox ID ="txtPass"  runat ="server" TextMode ="PassWord" Width="149px"></asp:TextBox></td>29             </tr>30             <tr>31                 <td>重复密码:</td><td class="auto-style1"><asp:TextBox ID ="txtQpass"  runat ="server" TextMode ="Password" Width="149px"></asp:TextBox></td>32             </tr>33             <tr>34                 <td>昵称:</td><td class="auto-style1"><asp:TextBox ID ="txtNickName"  runat ="server"></asp:TextBox></td>35             </tr>36             <tr>37                 <td>性别:</td><td class="auto-style1"><asp:RadioButton ID ="radnan" runat ="server" Text ="男" GroupName ="sex" Width ="80px"/><asp:RadioButton ID ="radnv" runat ="server" Text ="女" GroupName ="sex" Width ="80px"/></td>38             </tr>39             <tr>40                 <td>电话:</td><td class="auto-style1"><asp:TextBox ID ="txtPhone"  runat ="server"></asp:TextBox></td>41             </tr>42             <tr>43                 <td>E-mail:</td><td class="auto-style1"><asp:TextBox ID ="txtEamil"  runat ="server"></asp:TextBox></td>44             </tr>45             <tr>46                 <td>所在城市:</td><td class="auto-style1"><asp:TextBox ID ="txtCity"  runat ="server"></asp:TextBox></td>47             </tr>48             <tr>49                 <td><asp:Button ID ="btnregister" runat ="server" Text ="注册" OnClick ="btnregister_Click"/></td><td class="auto-style1"><asp:Button ID ="btnreturn" runat ="server" Text="返回" PostBackUrl ="~/Default.aspx" CausesValidation ="true"/></td>50             </tr>51         </table>52     </form>53 </body>54 </html>

register.aspx.cs后台代码:

  1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Web;  5 using System.Web.UI;  6 using System.Web.UI.WebControls;  7   8 using System.Data;  9 using System.Web.Security; 10 using System.Configuration; 11 using System.Data.SqlClient; 12 using System.Text.RegularExPRessions; 13  14 public partial class _Default : System.Web.UI.Page 15 { 16     protected void Page_Load(object sender, EventArgs e) 17     { 18     } 19  20     /// <summary> 21     /// 当焦点移出文本框时,判断用户是否存在,以及输入的字符格式是否符合规定. 22     /// </summary> 23     /// <param name="sender"></param> 24     /// <param name="e"></param> 25     protected void txtName_TextChanged(object sender, EventArgs e) 26     { 27         if (this.txtName.Text == "") 28         { 29             //如果没有输入用户名,则提示用户名不能为空. 30             this.labuser.Text = " * 用户名称不能为空!"; 31  32             //改变提示lable控件文字的颜色. 33             this.labuser.ForeColor = System.Drawing.Color.Red; 34         } 35         else 36         { 37             if (isnameFormar())//调用isnameFormar方法,判断用户名是否符合格式要求. 38             { 39                 if (isName())//调用iaName方法,判断用户名是否已经存在. 40                 { 41                     //已经注册,则提示. 42                     this.labuser.Text = " * 该用户已经注册!"; 43  44                     //改变提示lable控件文字的颜色. 45                     this.labuser.ForeColor = System.Drawing.Color.Red; 46                 } 47                 else 48                 { 49                     //没有注册,则提示可以注册. 50                     this.labuser.Text = " * 可以注册!"; 51  52                     //改变提示lable控件文字的颜色. 53                     this.labuser.ForeColor = System.Drawing.Color.Red; 54                 } 55             } 56             else 57             { 58                 //格式不对,则提示 59                 this.labuser.Text = " * 用户名格式不对!"; 60  61                 //改变提示lable控件文字的颜色. 62                 this.labuser.ForeColor = System.Drawing.Color.Red; 63             } 64         } 65     } 66  67     /// <summary> 68     /// 注册按钮的事件 69     /// </summary> 70     /// <param name="sender"></param> 71     /// <param name="e"></param> 72     protected void btnregister_Click(object sender, EventArgs e) 73     { 74         if (isnameFormar()) 75         { 76             if (isName()) 77             { 78                 this.labuser.Text = "用户名已经存在!"; 79  80                 this.labuser.ForeColor = System.Drawing.Color.Red; 81  82                 RegisterStartupScript("", "<script>alert('请输入正确信息!')</script>");//不报错,但是已过时,在.NET3.5和4.0中有更好的方法替代他们。 83             } 84             else 85             { 86                 //获取用户名 87                 string username = this.txtName.Text.Trim(); 88  89                 //获取用户填写的密码,并用md5进行加密. 90                 string userpass = FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtPass.Text, "MD5");//不报错,但是已过时,在.NET3.5和4.0中有更好的方法替代他们。 91  92                 //获取昵称 93                 string nickname = this.txtNickName.Text; 94  95                 //获取性别 96                 string sex = string.Empty; 97                 if (this.radnan.Checked) 98                 { 99                     sex = "男";100                 }101                 else if (this.radnv.Checked)102                 {103                     sex = "女";104                 }105                 else { }106 107                 //获取电话号码108                 string phone = this.txtPhone.Text;109 110                 //获取电子邮件地址111                 string Email = this.txtEamil.Text;112 113                 //获取所在城市114                 string city = this.txtCity.Text;115 116                 string strcon = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;//连接数据库.117 118                 using (SqlConnection con = new SqlConnection(strcon))119                 {120                     if (con.State == ConnectionState.Closed)121                     {122                         con.Open();123                     }124 125                     SqlParameter[] paras = new SqlParameter[] 126                     {127                         new SqlParameter("@username", username),128                         new SqlParameter("@userpass", userpass),129                         new SqlParameter("@nickname", nickname),130                         new SqlParameter("@sex", sex),131                         new SqlParameter("@phone", phone),132                         new SqlParameter("@Email", Email),133                         new SqlParameter("@city", city)134                     };135 136                     string sql = "insert into tb_User (userName, userPass, nickName, sex, phone, E_mail, city) values (@username, @userpass, @nickname, @sex, @phone, @Email, @city);";137 138                     SqlCommand cmd = new SqlCommand(sql, con);139 140                     cmd.Parameters.AddRange(paras); 1