Web用户管理之用户注册
首先,在MSSQL数据库(DataBase)中,创建一个表(Table),结构如下:
| 列名(Column) | 数据类型(DataType) | 长度(Length) | 备注 |
| id | uniqueidentifier | 16 | 主码(PrimaryKey) |
| username | nvartext | 20 | |
| userpwd | nvartext | 20 |
其次,我们选择 md5算法 对用户的密码进行加密。代码(Code)如下:
此页面为WebControl
RegUser.ascx
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="RegUser.ascx.cs"Inherits="WebApplication1.control.RegUser"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<LINK href="../style/css.css" type="text/css" rel="stylesheet">
<form id="form_reguser" method="post" runat="server">
<table id="table2" cellpadding="0" width="600" align="center" border="0">
<tr>
<td width="160" align="left" valign="top">
<TABLE id="Table_sub1" cellSpacing="0" cellPadding="0" width="140" border="0">
<TBODY>
<TR><td>
<table id="table1" cellSpacing="0" cellPadding="0" width="600" align="center" border="0">
<tr>
<td class="contexttitle" vAlign="bottom" align="center" height="100">注册用户<span id="Message" runat="server"></span></td>
</tr>
<tr>
<td>
<table cellSpacing="1" cellPadding="1" width="100%" align="center" border="0">
<tr>
<td align="center">用户名
<asp:textbox id="TextBoxUser" MaxLength="16" Runat="server"></asp:textbox><asp:requiredfieldvalidator id="RequiredFieldValidator1"
runat="server" ErrorMessage="请填写"
ControlToValidate="TextBoxUser">
</asp:requiredfieldvalidator>
</td>
</tr>
<tr>
<td align="center"><font color="red">密码初始值与用户名相同<br>
请及时修改您的密码</font></td>
</tr>
<tr>
<td align="center"><asp:Button ID="Buttonsubmit" Runat="server"Text="注册" Width="100"></asp:Button></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
Reguser.ascx.cs
namespace WebApplication1.control
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;
/// <summary>
/// RegUser 的摘要说明。
/// </summary>
public class RegUser : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Button Buttonsubmit;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.HtmlControls.HtmlGenericControl Message;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected System.Data.SqlClient.SqlCommand sqlCommand1;
protected System.Web.UI.WebControls.TextBox TextBoxUser;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
Message.InnerHtml="";
}
}
Web 窗体设计器生成的代码
private void Buttonsubmit_Click(object sender, System.EventArgs e)
{
string strUser=this.TextBoxUser.Text.Trim();
this.sqlCommand1.CommandText="select * from tbuser where username='"+strUser+"'";
try
{
this.sqlConnection1.Open();
SqlDataReader dr=this.sqlCommand1.ExecuteReader();
if(dr.HasRows)
{
this.Message.InnerHtml="<br><font size='4' color='red'>该用户名已经存在<br>"
+"请重新注册</font>";
}
else
{
this.sqlConnection1.Close();
Encoding ascii=Encoding.ASCII;
byte[] data=ascii.GetBytes(strUser);
System.Security.Cryptography.MD5 md5=new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] result=md5.ComputeHash(data);
string pwdmd5=Convert.ToBase64String(result);
string cmdText="Insert Into tbuser(username,userpwd) values('"+
strUser+"','"+pwdmd5+"')";
SqlCommand sqlCommand2=new SqlCommand(cmdText,this.sqlConnection1);
this.sqlConnection1.Open();
sqlCommand2.ExecuteNonQuery();
sqlCommand2.Dispose();
this.Message.InnerHtml="<br><font size='4' color='blue'>注册成功,"+"请尽快修改密码</font>";
}
}
catch(SqlException Ex)
{
this.Message.InnerHtml="<br><font size='4' color='red'>错误"+Ex.Message+"</font>";
}
finally
{
this.sqlCommand1.Dispose();
this.sqlConnection1.Close();
}
}
}
}


浙公网安备 33010602011771号