zl程序教程

您现在的位置是:首页 >  后端

当前栏目

注册页实现激活邮箱验证(asp.netc#)

c#NetASP注册 实现 激活 验证 邮箱
2023-06-13 09:14:51 时间
-----------注册页前台
复制代码代码如下:

<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="Registe.aspx.cs"Inherits="CSDN博客.Registe"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title></title>
<styletype="text/css">
.zhuyi
{
color:Red;
font-size:small;
}
#pwd-strong{
float:left;
margin:0px;
padding:0px;
list-style:none;
background-image:url(images/pwdstrong.gif);
background-repeat:no-repeat;
}
#pwd-strongli{
float:left;
padding:0px;
color:#ccc;
font-size:11px;
width:64px;
height:10px;
text-align:center;
padding-top:9px;
}
.pwds3{
background-position:-20px-70px;
}
.pwds2{
background-position:-20px-44px;
}
.pwds1{
background-position:-20px-18px;
}
#pwd-strongli.currs{
color:#000;
}
/*头部css样式*/
#head
{
margin:0;
}
#heada:hover
{
color:Red;
}
.ahead
{
color:black;
font-size:small;
text-decoration:none;
}
#lefthead
{
float:left;
margin:0;
}
#righthead
{
float:right;
margin:0;
}
/*头部css样式结束*/
/*信息部分开始*/
#body
{
/*
border:1pxsolidblack;
*/
position:absolute;
width:900px;
height:700px;
left:230px;
top:50px;
}
#picture
{
float:left;
}
#xinxi
{
border:1pxsolidpink;
width:850px;
height:440px;
position:absolute;
left:20px;
top:50px
}
#biaoti
{
position:absolute;
left:1px;
top:10px;
}
#xian
{
position:absolute;
left:20px;
top:50px;
}
#table
{
position:absolute;
/*
border:1pxsolidblack;
*/
left:50px;
top:90px;
}
/*信息部分结束*/
/*底部开始*/
#footer
{
/*
border:1pxsolidblack;
*/
position:absolute;
left:430px;
top:560px;
}
.buttom
{
margin:0px;
font-size:small;
}
.abuttom
{
text-decoration:none;
color:Black;
}
#footera:hover
{
color:Blue;
}
img
{
border:0px;
}
/*底部结束*/
</style>
<scriptsrc="emailvalidatorjs.js"type="text/javascript"></script>
<scriptsrc="jquery-1.9.1.js"type="text/javascript"></script>
<scripttype="text/javascript">
$(function(){
$("#txtPassword").keyup(function(){
varscore=testpass($(this).val());
if(score<34){
$("#pwd-strong").css("display","block");
$("#pwd-strong").addClass("pwds1");
$("li:first").addClass("currs");
}
elseif(score>=34&&score<68){
$("#pwd-strong").css("display","block");
$("#pwd-strong").removeClass();
$("#pwd-strong").addClass("pwds2");
$("li:eq(1)").addClass("currs");
}
else{
$("#pwd-strong").css("display","block");
$("#pwd-strong").removeClass();
$("#pwd-strong").addClass("pwds3");
$("li:last").addClass("currs");
}
})
})
</script>
</head>
<body>
<formid="form1"runat="server">
<!--内容-->
<div>
<!--头部-->
<divid="head">
<!--头部左-->
<divid="lefthead">
<ahref="http://www.csdn.net/"class="ahead">首页</a>
<ahref="http://news.csdn.net/"class="ahead">业界</a>
<ahref="http://mobile.csdn.net/"class="ahead">移动</a>
<ahref="http://cloud.csdn.net/"class="ahead">云计算</a>
<ahref="http://sd.csdn.net/"class="ahead">研发</a>
<ahref="http://bbs.csdn.net/"class="ahead">论坛</a>
<ahref="http://blog.csdn.net/"class="ahead">博客</a>
<ahref="http://download.csdn.net/"class="ahead">下载</a>
<aclass="ahead">更多</a>
</div>
<!--头部右-->
<divid="righthead">
<spanclass="zhuyi">你还未登陆!</span>|&nbsp
<ahref="http://www.csdn.net/"class="ahead">登陆</a>&nbsp&nbsp|&nbsp
<ahref="http://www.csdn.net/"class="ahead">注册</a>&nbsp&nbsp|&nbsp
<ahref="https://passport.csdn.net/help/faq"class="ahead">帮助</a>
</div>
</div>
<!--头部结束-->
<!--身体-->
<divid="body">
<!--图片-->
<divid="picture">
<imgsrc="images/zhuce.gif"/>
</div>
<!--信息-->
<divid="xinxi">
<divid="biaoti">
<imgsrc="images/zhuce1.gif"/>
</div>
<divid="xian">
<imgsrc="images/zhuce2.png"/>
</div>
<divid="table">
<table>
<!--用户名-->
<tr>
<td><spanclass="zhuyi">*</span>用户名:</td>
<td>
<asp:TextBoxID="txtName"runat="server"ControlToValidate="TextBox1"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidatorID="RequiredFieldValidator1"runat="server"
ErrorMessage="用户名不能为空"ControlToValidate="txtName"Display="Dynamic"
Font-Size="15px"ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr><td></td><tdcolspan="2">
<labelstyle="color:#c0c0c0;font-size:small;">(字母、数字或下划线组合。)</label></td></tr>
<!--密码-->
<tr>
<td><spanclass="zhuyi">*</span>密码:</td>
<td>
<asp:TextBoxID="txtPassword"runat="server"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidatorID="RequiredFieldValidator2"runat="server"
ControlToValidate="txtPassword"Display="Dynamic"ErrorMessage="不能为空"
Font-Size="15px"ForeColor="Red"></asp:RequiredFieldValidator>
<div><ulid="pwd-strong"style="display:none;"><li>弱</li><li>中</li><li>强</li></ul></div></td>
</tr>
<tr><td></td><tdcolspan="2">
<labelstyle="color:#c0c0c0;font-size:small;">(为了您的帐户安全,强烈建议您的密码使用字符+数字等多种不同类型的组合,并且密码长度大于5位。)</label></td></tr>
<!--再次输入密码-->
<tr>
<td><spanclass="zhuyi">*</span>再次输入密码:</td>
<td>
<asp:TextBoxID="txtPasswordAgain"runat="server"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidatorID="RequiredFieldValidator3"runat="server"
ControlToValidate="txtPassword"Display="Dynamic"ErrorMessage="不能为空"
ForeColor="Red"Font-Size="15px"></asp:RequiredFieldValidator>
<asp:CompareValidatorID="CompareValidator1"runat="server"
ErrorMessage="两次输入的密码不一致"ControlToCompare="txtPassword"
ControlToValidate="txtPasswordAgain"Display="Dynamic"ForeColor="Red"
Font-Size="15px"></asp:CompareValidator></td>
</tr>
<tr><td></td><tdcolspan="2">
<labelstyle="color:#c0c0c0;font-size:small;">(请确保密码正确)</label></td></tr>
<!--E-mail-->
<tr>
<td><spanclass="zhuyi">*</span>E-mail</td>
<td>
<asp:TextBoxID="txtEmail"runat="server"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidatorID="RequiredFieldValidator4"runat="server"
ControlToValidate="txtEmail"Display="Dynamic"ErrorMessage="不能为空"
ForeColor="Red"Font-Size="15px"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidatorID="RegularExpressionValidator1"runat="server"
ControlToValidate="txtEmail"Display="Dynamic"ErrorMessage="邮箱格式不正确"
ValidationExpression="\w+([-+."]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ForeColor="Red"Font-Size="15px"></asp:RegularExpressionValidator>
</td>
</tr>
<tr><td></td><tdcolspan="2">
<labelstyle="color:#c0c0c0;font-size:small;">(请填写您常用的邮箱)</label></td></tr>
<!--验证码-->
<tr>
<td><spanclass="zhuyi">*</span>校验码:</td>
<td>
<asp:TextBoxID="TextBox5"runat="server"></asp:TextBox></td>
<td>
<imgsrc="checkCode.aspx"onclick="this.src="checkCode.aspx?aaa="+newDate()"alt=""/>图片看不清?点击重新得到验证码
<asp:RequiredFieldValidatorID="RequiredFieldValidator5"runat="server"
ControlToValidate="TextBox5"Display="Dynamic"ErrorMessage="不能为空"></asp:RequiredFieldValidator>
</td>
</tr>
<tr><td></td><tdcolspan="2">
<spanclass="zhuyi">(如您连续输入不对验证码,请检查您的浏览器是否禁用了Cookie。<ahref="http://passport.csdn.net/help/faq">如何启用Cookie?</a>)</span></td></tr>
<tr>
<!--条款-->
<td><spanclass="zhuyi">*</span>注册条款:</td>
<tdcolspan="2">
<asp:CheckBoxID="CheckBox1"runat="server"
oncheckedchanged="CheckBox1_CheckedChanged"Text="我已仔细阅读并接受"/><ahref="http://passport.csdn.net/help/terms">CSDN注册条款</a>。</td>
</tr>
<!--注册提交-->
<tr><td></td><td></td></tr>
<tr><td></td><td></td></tr>
<tr>
<td></td>
<tdcolspan="2">
<asp:ImageButtonID="ImageButton1"runat="server"
ImageUrl="~/images/zhuce.jpg"onclick="ImageButton1_Click"/>
<asp:LabelID="lbinfo"runat="server"ForeColor="Red"></asp:Label>
</td>
</tr>
</table>
<div>
</div>
</div>
</div>
</div>
<!--身体结束-->
<!--底部-->
<divid="footer"align="center">
<divclass="buttom">
<ahref="http://www.csdn.net/company/about.html"class="abuttom">公司简介</a>|
<ahref="http://www.csdn.net/company/recruit.html"class="abuttom">招贤纳士</a>|
<ahref="http://www.csdn.net/company/marketing.html"class="abuttom">广告服务</a>|
<ahref="http://www.csdn.net/company/account.html"class="abuttom">银行汇款账号</a>|
<ahref="http://www.csdn.net/company/contact.html"class="abuttom">联系方式</a>|
<ahref="http://www.csdn.net/company/statement.html"class="abuttom">版权声明</a>|
<ahref="http://www.csdn.net/company/layer.html"class="abuttom">法律顾问</a>|
<ahref=""class="abuttom">问题报告</a>
</div>
<pclass="buttom">京&nbsp&nbspICP&nbsp&nbsp证&nbsp&nbsp070598&nbsp&nbsp号</p>
<pclass="buttom">北京创新乐知信息技术有限公司&nbsp&nbsp版权所有</p>
<pclass="buttom"><imgsrc="images/zhuce4.gif"/>联系邮箱:webmaster(at)csdn.net</p>
<pclass="buttom">Copyright&nbsp©&nbsp1999-2012,&nbspCSDN.NET,&nbspAll&nbspRights&nbspReserved</p>
<div>
<ahref="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001032100010"class="abuttom"><imgsrc="images/denglu2.gif"/></a>
<ahref="https://trustsealinfo.verisign.com/splash?form_file=fdf/splash.fdf&dn=passport.csdn.net&lang=zh_cn"class="abuttom"><imgsrc="images/denglu1.gif"/></a>
</div>

------注册页后台
复制代码代码如下:

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Text;
usingSystem.Net.Mail;
usingSystem.Net;
usingSystem.Data.SqlClient;
namespaceCSDN博客
{
publicpartialclassRegiste:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
}
protectedvoidCheckBox1_CheckedChanged(objectsender,EventArgse)
{
}
publicvoidsendMail(stringemail,stringactiveCode)
{
MailMessagemailMsg=newMailMessage();
mailMsg.From=newMailAddress("15031259715@163.com");
mailMsg.To.Add(email);
mailMsg.Subject="请激活注册";
intnumber=number1();
StringBuildercontentBuilder=newStringBuilder();
contentBuilder.Append("请单击以下链接完成激活");
contentBuilder.Append("<ahref="http://localhost:15464/cheng.aspx?activecode="+activeCode+"&id="+number+"">激活</a>");
mailMsg.Body=contentBuilder.ToString();//拼接字符串
mailMsg.IsBodyHtml=true;
SmtpClientclient=newSmtpClient();
//发件方服务器地址
client.Host="smtp.163.com";
client.Port=25;
//mailMsg.IsBodyHtml=true;
NetworkCredentialcredetial=newNetworkCredential();
credetial.UserName="15031259715";
credetial.Password="wangjing911214++";
client.Credentials=credetial;
client.Send(mailMsg);
}
publicintnumber1()
{
CSDN博客.BLL.T_Usercount=newBLL.T_User();
inta=count.GetRecordCount("");
returna;
}
protectedvoidImageButton1_Click(objectsender,ImageClickEventArgse)
{
CSDN博客.Model.T_Usermuser=newModel.T_User();
muser.Name=txtName.Text;
muser.Password=txtPassword.Text;
muser.E_Mail=txtEmail.Text;
stringactivecode=Guid.NewGuid().ToString().Substring(0,8);
muser.ActiveCode=activecode;//生成激活码
CSDN博客.BLL.T_Userbuser=newBLL.T_User();
if(buser.Add(muser)>0)
{
sendMail(txtEmail.Text,activecode);//给注册用户发邮件
lbinfo.Text="保存成功";
}
else{lbinfo.Text="保存失败";}
}
}
}
</div>
<!--底部结束-->
</div>
</form>
</body>
</html>

------------激活验证
复制代码代码如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Data.SqlClient;
namespaceCSDN博客
{
publicpartialclasscheng:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
//取出参数id
intid=Convert.ToInt32(Request["id"]);
stringactiveCode=Request["activecode"].ToString();
//2判断id为id的记录是否存在
//连接数据库
stringconStr="datasource=LOVE-PC\\SQLEXPRESSPC;initialcatalog=Blogs;userid=sa;password=admin";
intnumber;
using(SqlConnectioncon=newSqlConnection(conStr))
{
stringsql="selectcount(*)fromT_UserwhereId=@id";
using(SqlCommandcmd=newSqlCommand(sql,con))
{
con.Open();
cmd.Parameters.AddWithValue("@id",id);
number=Convert.ToInt32(cmd.ExecuteScalar());
}
}
if(number>0)
{
//如果该用户存在取出ActiveCode字段进行比较。如果一样,把Active字段修改为true
//连接数据库
stringAC;
using(SqlConnectioncon=newSqlConnection(conStr))
{
stringsql="selectActiveCodefromT_UserwhereId=@id";
using(SqlCommandcmd=newSqlCommand(sql,con))
{
con.Open();
cmd.Parameters.AddWithValue("@id",id);
AC=cmd.ExecuteScalar().ToString();;
}
}
if(activeCode==AC)
{
Response.Write("激活成功!<ahref="denglu.aspx">返回登录</a>");
using(SqlConnectioncon=newSqlConnection(conStr))
{
stringsql="updateT_UsersetActive=1whereId=@id";
using(SqlCommandcmd=newSqlCommand(sql,con))
{
con.Open();
cmd.Parameters.AddWithValue("@id",id);
number=Convert.ToInt32(cmd.ExecuteScalar());
}
}
}
else
{
Response.Write("用户已存在,但是激活码错误!");
}
}
else
{
Response.Write("用户不存在,还没注册成功!");
}
}
}
}

实现如下效果: