# 会员注册与登录模块

来源:互联网 发布:centos7 网络配置文件 编辑:程序博客网 时间:2024/04/29 19:56

首先分别新建三个网页,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); 141 142                     if (Convert.ToInt32(cmd.ExecuteNonQuery()) > 0)143                     {144                         RegisterStartupScript("", "<script>alert('注册成功!')</script>");145 146                         this.txtName.Text = this.txtPass.Text = this.txtQpass.Text = "";147                         this.txtNickName.Text = this.txtPhone.Text = this.txtEamil.Text = this.txtCity.Text = "";148                         this.labuser.Text = "";149                     }150                     else151                     {152                         RegisterStartupScript("", "<script>alert('请输入正确格式!')</script>");153                     }154                 }155             }156         }157         else158         {159             RegisterStartupScript("", "<script>alert('请输入正确格式!')</script>");160         }161     }162 163     /// <summary>164     /// 判断用户名是否符合格式要求.165     /// </summary>166     /// <returns></returns>167     public bool isnameFormar()168     {169         bool flag = false;170 171         //设置正则表达式.(使用此对象要导入 System.Text.RegularExpressions命名空间)172         Regex re = new Regex("^\\w+$");173 174         if (re.IsMatch(this.txtName.Text.Trim()))175         {176             flag = true;//返回flag为真.177         }178 179         return flag;180     }181 182     /// <summary>183     /// 判断用户名是否已经存在184     /// </summary>185     /// <returns></returns>186     public bool isName()187     {188         bool flag = false;189 190         string strcon = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;//连接数据库.191 192         using (SqlConnection con = new SqlConnection(strcon))193         {194             if (con.State == ConnectionState.Closed)195             {196                 con.Open();197             }198 199             SqlParameter[] paras = new SqlParameter[]200             {201                 new SqlParameter("@userName", this.txtName.Text.Trim())202             };203 204             string sql = "select * from tb_User where userName = @userName ";205 206             SqlCommand cmd = new SqlCommand(sql, con);207 208             cmd.Parameters.AddRange(paras);209 210             if ( Convert.ToInt32(cmd.ExecuteScalar()) > 0)//如果返回值大于0,则表示用户已经存在.211             {212                 flag = true;213             }214         }215 216         return flag;217     }218 }
复制代码

 

以上是注册页面的编写,下面是登录页面的编写:

 

 

login.aspx代码:

复制代码
 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" Debug ="true"%> 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         #imagecode {11             text-align:left;12         }13     </style>14 </head>15 <body>16     <form id="form1" runat="server">17     <div>18     <table>19         <tr>20             <td>用户名:</td><td><asp:TextBox ID ="txtname" runat ="server"></asp:TextBox></td>21         </tr>22         <tr>23             <td>密码:</td><td><asp:TextBox ID ="txtpass" runat ="server" TextMode ="Password"></asp:TextBox></td>24         </tr>25         <tr>26             <td>验证码:</td><td><asp:TextBox ID ="yzm" runat ="server" Width ="60px"></asp:TextBox><img id ="imagecode" src ="yzm.aspx" alt ="看不清!请点击刷新!" onclick ="this.src=this.src+'?'" />27         </tr>28         <tr>29             <td style ="text-align:center"><asp:Button ID ="txtCode" runat ="server" Text ="登陆" OnClick ="txtCode_Click1"/></td><td style ="text-align:center"><asp:Button ID ="cz" runat ="server" Text ="重置" /></td>30         </tr>31     </table>32     </div>33     </form>34 </body>35 </html>
复制代码

 

login.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.Data.SqlClient;11 using System.Configuration;12 13 public partial class login : System.Web.UI.Page14 {15     protected void Page_Load(object sender, EventArgs e)16     {17         18     }19 20     protected void txtCode_Click1(object sender, EventArgs e)21     {22         string code = this.yzm.Text;23 24         if (Request.Cookies["checkcode"].Value == code)25         {26             string strcon = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;27 28             using (SqlConnection con = new SqlConnection(strcon))29             {30                 if (con.State == ConnectionState.Closed)31                 {32                     con.Open();33                 }34 35                 string pass = FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtpass.Text, "MD5");36 37                 SqlParameter[] paras = new SqlParameter[]38                 {39                     new SqlParameter("@pass", pass),40                     new SqlParameter("@name", this.txtname.Text)41                 };42 43                 string sql = "select * from tb_User where userName = @name and userPass = @pass";44 45                 SqlCommand cmd = new SqlCommand(sql, con);46 47                 cmd.Parameters.AddRange(paras);48 49                 if (Convert.ToInt32(cmd.ExecuteScalar()) > 0)50                 {51                     RegisterStartupScript("", "<script>alert('登录成功!')</script>");52                     //清空文本框53                     txtpass.Text = yzm.Text = txtname.Text = "";54                 }55                 else56                 {57                     RegisterStartupScript("", "<script>alert('用户名或密码错误!')</script>");58                 }59             }60         }61         else62         {63             RegisterStartupScript("", "<script>alert('验证码错误!')</script>");64         }65     }66 }
复制代码

 

以上是登录页面的编写,下面是验证码的编写:

 

yzm.aspx代码(并没有使用一般处理程序):

  -yzm.aspx前台代码不需要编写。

 

yzm.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.Drawing;  9  10 public partial class yzm : System.Web.UI.Page 11 { 12     protected void Page_Load(object sender, EventArgs e) 13     { 14         createyzm(random()); 15     } 16  17     /// <summary> 18     /// 这个方法是用于生成随机数 19     /// </summary> 20     /// <returns>生成的随机数</returns> 21     public string random() 22     { 23         int number;//创建一个整型变量,用于存储的单个随机数. 24  25         char a = new char();//创建一个字符变量,用于存储有整型转换成字符型的变量. 26  27         string b = string.Empty;//创建一个字符串变量,用于存储生成的随机数字符串. 28  29         Random rm = new Random();//创建一个随机数对象rm, 用于获取随机数. 30  31         //循环,用于生成单个随机数,以及把单个随机数,拼合成字符串. 32         for (int i = 0; i < 4; i++) 33         { 34             number = rm.Next();//生成随机数. 35  36             a = (char)('0' + (char)(number % 10));//把生成的随机数,无论是多大,只要除于10,得到的余数都是个位数,然后把他们转换成char类型的字符. 37  38             b += a.ToString();//把获得的单个字符,累加成一个字符创,其中一定要把,字符类型,转换成字符串类型. 39         } 40  41         Response.Cookies.Add(new HttpCookie("checkcode", b));//把生成的随机数字符串,放到创建的名为checkcode的cookies中,以便于,其他页面的调用时获取. 42  43         return b;//同时把生成的随机数返回(传出),以便于验证码制作. 44     } 45  46     /// <summary> 47     /// 这个方法是用于制作验证码 48     /// </summary> 49     /// <param name="sjs">接收random()方法生成的随机数</param> 50     public void createyzm(string sjs) 51     { 52         if (null == sjs && sjs.Trim() == string.Empty) 53         { 54             return; 55         } 56  57         System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling(sjs.Length * 12.5), 22);//方法的重载,用于制定的大小初始化System.Drawing.Bitmap类的新实例,第一个参数是宽,第二个参数是高,单位是像素. 58  59         Graphics g = Graphics.FromImage(image);//从指定的 System.Drawing.Image 创建新的 System.Drawing.Graphics。 60  61         try 62         { 63             Random random = new Random();//创建随机数生成器. 64  65             g.Clear(Color.White);//清空背景色. 66  67             //绘制噪音线 68             for (int i = 0; i <10; i++) 69             { 70                 int x1 = random.Next(image.Width); 71                 int x2 = random.Next(image.Width); 72                 int y1 = random.Next(image.Height); 73                 int y2 = random.Next(image.Height); 74  75                 g.DrawLine(new Pen(Color.FromArgb( random.Next())), x1, x2, y1, y2);//绘制一条连接由坐标对指定的两个点的线条。颜色是随机的使用color.fromArgb(random.Next())。 76             } 77  78             Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));//设置文本的字体、字号和字形。 79  80             System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Width), Color.Blue, Color.OliveDrab, 1.2f, true);//设置绘制线条的线性渐变. 81  82             g.DrawString(sjs, font, brush, 2, 2);//在指定位置并且用指定的 System.Drawing.Brush 和 System.Drawing.Font 对象绘制指定的文本字符串。 83  84             //绘制噪音点 85             for (int i = 0; i < 100; i++) 86             { 87                 int x = random.Next(image.Width); 88                 int y = random.Next(image.Height); 89  90                 image.SetPixel(x, y, Color.FromArgb(random.Next()));//绘制随机点,其中颜色也是随机的. 91             } 92  93             //绘制验证码边框线 94             //参数一:确定矩形的颜色,宽度,和样式。 95             //参数二:要绘制的矩形的左上角的x坐标。 96             //参数三:要绘制矩形的左上角y的坐标。 97             //参数四:要绘制矩形的宽度。 98             //参数五:要绘制矩形的高度。 99             g.DrawRectangle(new Pen(Color.Black), 0, 0, image.Width - 1, image.Height - 1);100 101             //输出到页面上102             System.IO.MemoryStream ms = new System.IO.MemoryStream();//创建支持存储区为内存的流。103 104             image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);//将图像以指定的格式保存到指定的流中。其中ImageFormat的作用是指定图像的文件格式。105 106             Response.ClearContent();//清除缓冲区流中的所有内容输出。107 108             Response.ContentType = "image/Gif";//获取或设置输出流的 http mime 类型。109 110             Response.BinaryWrite(ms.ToArray());//将二进制字符串写入 http 输出流。111         }112         catch (Exception ex)113         {114             throw ex;115         }116         finally117         {118             g.Dispose();//释放其使用的资源119             image.Dispose();//释放其使用的资源.120         }121     }122 }
复制代码
0 0
原创粉丝点击