ASP.NET 实现注册用户 邮箱激活
2011-12-20 21:13
791 查看
问题的提出:
当一个用户注册后,首先他不能立即使用用户名和密码登录,需要使用邮箱验证激活后方能使用。
Code:
EmailInfo.aspx(起始页文件)
前台代码
代码
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><form id="form1"
runat="server">
<div>
<table cellpadding="3"
width="550">
<tr>
<td colspan="3"
>
<b>Fill
in Your Details</b>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr align="center">
<td>
Your Name :
</td>
<td align="left">
<asp:textbox id="txtName"
runat="server"></asp:textbox>
</td>
</tr>
<tr align="center">
<td>
User Name :
</td>
<td align="left">
<asp:textbox id="txtUserName"
runat="server"></asp:textbox>
</td>
</tr>
<tr align="center">
<td>
Email Address :
</td>
<td align="left">
<asp:textbox id="txtEmail"
runat="server"></asp:textbox>
</td>
</tr>
<tr align="center">
<td
>
Password :
</td>
<td align="left">
<asp:textbox id="txtPassword"
textmode="Password" runat="server"></asp:textbox>
</td>
</tr>
<tr align="center">
<td>
Confirm Password :
</td>
<td align="left">
<asp:textbox id="txtConfirmPassword"
textmode="Password" runat="server"></asp:textbox>
</td>
<td align="left">
<asp:CompareValidator ID="CompareValidator1"
runat="server"
ErrorMessage="Password not match" ControlToCompare="txtPassword"
ControlToValidate="txtConfirmPassword" Display="Dynamic"></asp:CompareValidator>
</td>
</tr>
<tr align="right">
<td colspan="3">
<asp:Button ID="btnRegister"
runat="server" Text="Register"
onclick="btnRegister_Click"
/>
</td>
</tr>
</table>
</div>
<asp:Label ID="Label1"
runat="server" Text="Label"></asp:Label>
</form>
后台代码
代码
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Net.Mail;
using System.Net;
using System.Resources;
using System.IO;
namespace EmailActive
{
public
partial class EmailInfo : System.Web.UI.Page
{
protected
void Page_Load(object sender, EventArgs e)
{
}
protected
void btnRegister_Click(object sender, EventArgs e)
{
string iUserID
= Users.RegisterClient(txtUserName.Text.Trim(), txtEmail.Text.Trim(), txtPassword.Text.Trim(), txtName.Text.Trim()).ToString();
string sData
= File.ReadAllText(Server.MapPath("~/NewMemberEmail.txt"));
Label1.Text = iUserID;
Response.Write(sData);
sData = sData.Replace("[Name]",
txtName.Text.Trim());
sData = sData.Replace("[LINK]",
"http://localhost:18277(这里根据VS生成地址修改)"
+ "/Activate.aspx?UserID="
+
iUserID +
"&UN="
+ Server.UrlEncode(txtUserName.Text.Trim()));
//使用Server.UrlEncode()解决QueryString()传递中文乱码问题
sData = sData.Replace("[UserName]",
txtUserName.Text.Trim());
sData = sData.Replace("[Pwd]",
txtPassword.Text.Trim());
SMTPManager.SendEmail("zfj123589@gmail.com", txtName.Text.Trim(), txtEmail.Text.Trim(),
sData, "New Member Activation",
false);
Response.Redirect("Login.aspx");
}
public
class Users
{
public
static int RegisterClient(string UserName,
string EmailAddress,
string Password,
string Name)
{
string strConn
= ConfigurationManager.ConnectionStrings["Reg"].ConnectionString;
SqlConnection conn =
new SqlConnection(strConn);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText =
"Insert into tblUsers (UserName, Email, Password, Name) Values (@UserName, @EmailAddress, @Password, @Name)"
+
"select @UserID = SCOPE_IDENTITY()";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@UserName", SqlDbType.VarChar,
50);
cmd.Parameters["@UserName"].Value
= UserName;
cmd.Parameters.Add("@EmailAddress", SqlDbType.VarChar,
100);
cmd.Parameters["@EmailAddress"].Value
= EmailAddress;
cmd.Parameters.Add("@Password", SqlDbType.VarChar,
50);
cmd.Parameters["@Password"].Value
= Password;
cmd.Parameters.Add("@Name", SqlDbType.VarChar,
100);
cmd.Parameters["@Name"].Value
= Name;
SqlParameter p = cmd.Parameters.Add("@UserID",
SqlDbType.Int);
p.Direction = ParameterDirection.Output;
int newUserID
= 0;
cmd.ExecuteNonQuery();
conn.Close();
newUserID = (int)p.Value;
return newUserID;
}
}
///
<summary>
/// SMTPManager发送邮件类
///
</summary>
///FROM 发送者邮箱地址, FromDisplayName 发送者名字, TO接受者邮箱地址, BODY邮件内容,SUBJECT 主题行, bIsHtml是否是HTML邮件
public
class SMTPManager
{
public SMTPManager()
{
}
public
static void SendEmail(string FROM,
string FromDisplayName,
string TO,
string BODY,
string SUBJECT,
bool bIsHtml)
{
MailMessage m =
new MailMessage();
m.From =
new MailAddress(FROM, FromDisplayName);
m.To.Add(TO);
m.Subject = SUBJECT;
m.Body = BODY;
m.BodyEncoding = System.Text.Encoding.UTF8;
m.IsBodyHtml = bIsHtml;
m.ReplyTo =
new MailAddress(FROM);
//smtp.126.com不可用,所有使用smtp.gmail.com
SmtpClient smtp =
new SmtpClient("smtp.gmail.com",
587);
smtp.Credentials =
new NetworkCredential("zfj123589",
"123zxcvb");
smtp.EnableSsl =
true;
smtp.Send(m);
}
}
}
}
Activate.aspx(激活页面)
前台代码
<form id="form1"
runat="server">
<div>
<asp:Label ID="lblMsg"
runat="server" Text="Label"></asp:Label>
</div>
</form>
后台代码:
代码
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace EmailActive
{
public
partial class Activate : System.Web.UI.Page
{
protected
void Page_Load(object sender, EventArgs e)
{
string sUserID
= string.Empty;
string sID
= Request.QueryString["UserID"].Trim();
lblMsg.Text = sID;
//Server.UrlDecode()对URL地址进行解密
string sUserName
= Server.UrlDecode(Request.QueryString["UN"]).ToString().Trim();
string strConn
= ConfigurationManager.ConnectionStrings["Reg"].ConnectionString;
SqlConnection conn =
new SqlConnection(strConn);
SqlCommand cmd = conn.CreateCommand();
try
{
conn.Open();
cmd.CommandText =
"Select UserID from tblUsers where UserName = '"
+ sUserName
+ "'";
sUserID = cmd.ExecuteScalar().ToString().Trim();
}
finally
{
conn.Close();
}
if (String.Compare(sID, sUserID)
== 0)
{
conn.Open();
SqlCommand sqlCmd = conn.CreateCommand();
cmd.CommandText =
"Update tblUsers set Verified = 1 where UserName = '"
+ sUserName
+ "'";
string UserID
= cmd.ExecuteNonQuery().ToString();
conn.Close();
Response.Redirect("Login.aspx");
}
else
{
lblMsg.Text =
"Failed to activate your account. Please contact zfj123589@gmail.com<br>";
}
}
}
}
Login.aspx(登录界面)
前台代码:
代码
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><form id="form1"
runat="server">
<div>
<table id="RegTable"
cellpadding="2">
<tr>
<td>
UserName:
</td>
<td>
<asp:TextBox ID="txtUserName"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<asp:TextBox ID="txtPassword"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnLog"
runat="server" Text="Log
In" onclick="btnLog_Click"
/>
</td>
<td>
<asp:Label ID="lblShow"
runat="server" ForeColor="Red"
Text="You are not activated..Please do it.."></asp:Label>
</td>
</tr>
</table>
</div>
</form>
后台代码:
代码
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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 using System.Data;
8 using System.Data.SqlClient;
9 using System.Configuration;
10
11 namespace EmailActive
12 {
13 public
partial
class Login : System.Web.UI.Page
14 {
15 protected
void Page_Load(object sender, EventArgs e)
16 {
17 lblShow.Visible
= false;
18 }
19
20 protected
void btnLog_Click(object sender, EventArgs e)
21 {
22 string strConn
= ConfigurationManager.ConnectionStrings["Reg"].ConnectionString;
23 SqlConnection conn
= new SqlConnection(strConn);
24 conn.Open();
25 SqlCommand cmd
= conn.CreateCommand();
26 cmd.CommandText
= "Select Verified from tblUsers where UserName = '"
+ txtUserName.Text
+ "' and Password = '"
+ txtPassword.Text
+ "'";
27 string strVer
= cmd.ExecuteScalar().ToString();
28 conn.Close();
29
30 //判断Verified字段是否为1如果为1说明已经激活..
31 if (String.Compare(strVer,
"1")
== 0)
32 {
33 Response.Redirect("Success.aspx");
34
35 }
36 else
37 {
38 lblShow.Visible
= true;
39 }
40
41
42 }
43 }
44 }
45
Success.aspx(成功激活后的界面) 不介绍了(就一个标签显示成功),看源代码..
Web.config中数据库连接自己修改下。
注意:发送邮件时,发现smtp.126.com,smtp.163.com不能使用,于是使用smtp.gmail.com发送邮件。
通过这个例子学到不少东西, 发送邮件,QueryString传值,解决乱码问题。。
在WIN 7 + Visual Web Develop 2008调试通过。
当一个用户注册后,首先他不能立即使用用户名和密码登录,需要使用邮箱验证激活后方能使用。
Code:
EmailInfo.aspx(起始页文件)
前台代码
代码
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><form id="form1"
runat="server">
<div>
<table cellpadding="3"
width="550">
<tr>
<td colspan="3"
>
<b>Fill
in Your Details</b>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr align="center">
<td>
Your Name :
</td>
<td align="left">
<asp:textbox id="txtName"
runat="server"></asp:textbox>
</td>
</tr>
<tr align="center">
<td>
User Name :
</td>
<td align="left">
<asp:textbox id="txtUserName"
runat="server"></asp:textbox>
</td>
</tr>
<tr align="center">
<td>
Email Address :
</td>
<td align="left">
<asp:textbox id="txtEmail"
runat="server"></asp:textbox>
</td>
</tr>
<tr align="center">
<td
>
Password :
</td>
<td align="left">
<asp:textbox id="txtPassword"
textmode="Password" runat="server"></asp:textbox>
</td>
</tr>
<tr align="center">
<td>
Confirm Password :
</td>
<td align="left">
<asp:textbox id="txtConfirmPassword"
textmode="Password" runat="server"></asp:textbox>
</td>
<td align="left">
<asp:CompareValidator ID="CompareValidator1"
runat="server"
ErrorMessage="Password not match" ControlToCompare="txtPassword"
ControlToValidate="txtConfirmPassword" Display="Dynamic"></asp:CompareValidator>
</td>
</tr>
<tr align="right">
<td colspan="3">
<asp:Button ID="btnRegister"
runat="server" Text="Register"
onclick="btnRegister_Click"
/>
</td>
</tr>
</table>
</div>
<asp:Label ID="Label1"
runat="server" Text="Label"></asp:Label>
</form>
后台代码
代码
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Net.Mail;
using System.Net;
using System.Resources;
using System.IO;
namespace EmailActive
{
public
partial class EmailInfo : System.Web.UI.Page
{
protected
void Page_Load(object sender, EventArgs e)
{
}
protected
void btnRegister_Click(object sender, EventArgs e)
{
string iUserID
= Users.RegisterClient(txtUserName.Text.Trim(), txtEmail.Text.Trim(), txtPassword.Text.Trim(), txtName.Text.Trim()).ToString();
string sData
= File.ReadAllText(Server.MapPath("~/NewMemberEmail.txt"));
Label1.Text = iUserID;
Response.Write(sData);
sData = sData.Replace("[Name]",
txtName.Text.Trim());
sData = sData.Replace("[LINK]",
"http://localhost:18277(这里根据VS生成地址修改)"
+ "/Activate.aspx?UserID="
+
iUserID +
"&UN="
+ Server.UrlEncode(txtUserName.Text.Trim()));
//使用Server.UrlEncode()解决QueryString()传递中文乱码问题
sData = sData.Replace("[UserName]",
txtUserName.Text.Trim());
sData = sData.Replace("[Pwd]",
txtPassword.Text.Trim());
SMTPManager.SendEmail("zfj123589@gmail.com", txtName.Text.Trim(), txtEmail.Text.Trim(),
sData, "New Member Activation",
false);
Response.Redirect("Login.aspx");
}
public
class Users
{
public
static int RegisterClient(string UserName,
string EmailAddress,
string Password,
string Name)
{
string strConn
= ConfigurationManager.ConnectionStrings["Reg"].ConnectionString;
SqlConnection conn =
new SqlConnection(strConn);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText =
"Insert into tblUsers (UserName, Email, Password, Name) Values (@UserName, @EmailAddress, @Password, @Name)"
+
"select @UserID = SCOPE_IDENTITY()";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@UserName", SqlDbType.VarChar,
50);
cmd.Parameters["@UserName"].Value
= UserName;
cmd.Parameters.Add("@EmailAddress", SqlDbType.VarChar,
100);
cmd.Parameters["@EmailAddress"].Value
= EmailAddress;
cmd.Parameters.Add("@Password", SqlDbType.VarChar,
50);
cmd.Parameters["@Password"].Value
= Password;
cmd.Parameters.Add("@Name", SqlDbType.VarChar,
100);
cmd.Parameters["@Name"].Value
= Name;
SqlParameter p = cmd.Parameters.Add("@UserID",
SqlDbType.Int);
p.Direction = ParameterDirection.Output;
int newUserID
= 0;
cmd.ExecuteNonQuery();
conn.Close();
newUserID = (int)p.Value;
return newUserID;
}
}
///
<summary>
/// SMTPManager发送邮件类
///
</summary>
///FROM 发送者邮箱地址, FromDisplayName 发送者名字, TO接受者邮箱地址, BODY邮件内容,SUBJECT 主题行, bIsHtml是否是HTML邮件
public
class SMTPManager
{
public SMTPManager()
{
}
public
static void SendEmail(string FROM,
string FromDisplayName,
string TO,
string BODY,
string SUBJECT,
bool bIsHtml)
{
MailMessage m =
new MailMessage();
m.From =
new MailAddress(FROM, FromDisplayName);
m.To.Add(TO);
m.Subject = SUBJECT;
m.Body = BODY;
m.BodyEncoding = System.Text.Encoding.UTF8;
m.IsBodyHtml = bIsHtml;
m.ReplyTo =
new MailAddress(FROM);
//smtp.126.com不可用,所有使用smtp.gmail.com
SmtpClient smtp =
new SmtpClient("smtp.gmail.com",
587);
smtp.Credentials =
new NetworkCredential("zfj123589",
"123zxcvb");
smtp.EnableSsl =
true;
smtp.Send(m);
}
}
}
}
Activate.aspx(激活页面)
前台代码
<form id="form1"
runat="server">
<div>
<asp:Label ID="lblMsg"
runat="server" Text="Label"></asp:Label>
</div>
</form>
后台代码:
代码
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace EmailActive
{
public
partial class Activate : System.Web.UI.Page
{
protected
void Page_Load(object sender, EventArgs e)
{
string sUserID
= string.Empty;
string sID
= Request.QueryString["UserID"].Trim();
lblMsg.Text = sID;
//Server.UrlDecode()对URL地址进行解密
string sUserName
= Server.UrlDecode(Request.QueryString["UN"]).ToString().Trim();
string strConn
= ConfigurationManager.ConnectionStrings["Reg"].ConnectionString;
SqlConnection conn =
new SqlConnection(strConn);
SqlCommand cmd = conn.CreateCommand();
try
{
conn.Open();
cmd.CommandText =
"Select UserID from tblUsers where UserName = '"
+ sUserName
+ "'";
sUserID = cmd.ExecuteScalar().ToString().Trim();
}
finally
{
conn.Close();
}
if (String.Compare(sID, sUserID)
== 0)
{
conn.Open();
SqlCommand sqlCmd = conn.CreateCommand();
cmd.CommandText =
"Update tblUsers set Verified = 1 where UserName = '"
+ sUserName
+ "'";
string UserID
= cmd.ExecuteNonQuery().ToString();
conn.Close();
Response.Redirect("Login.aspx");
}
else
{
lblMsg.Text =
"Failed to activate your account. Please contact zfj123589@gmail.com<br>";
}
}
}
}
Login.aspx(登录界面)
前台代码:
代码
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><form id="form1"
runat="server">
<div>
<table id="RegTable"
cellpadding="2">
<tr>
<td>
UserName:
</td>
<td>
<asp:TextBox ID="txtUserName"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<asp:TextBox ID="txtPassword"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnLog"
runat="server" Text="Log
In" onclick="btnLog_Click"
/>
</td>
<td>
<asp:Label ID="lblShow"
runat="server" ForeColor="Red"
Text="You are not activated..Please do it.."></asp:Label>
</td>
</tr>
</table>
</div>
</form>
后台代码:
代码
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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 using System.Data;
8 using System.Data.SqlClient;
9 using System.Configuration;
10
11 namespace EmailActive
12 {
13 public
partial
class Login : System.Web.UI.Page
14 {
15 protected
void Page_Load(object sender, EventArgs e)
16 {
17 lblShow.Visible
= false;
18 }
19
20 protected
void btnLog_Click(object sender, EventArgs e)
21 {
22 string strConn
= ConfigurationManager.ConnectionStrings["Reg"].ConnectionString;
23 SqlConnection conn
= new SqlConnection(strConn);
24 conn.Open();
25 SqlCommand cmd
= conn.CreateCommand();
26 cmd.CommandText
= "Select Verified from tblUsers where UserName = '"
+ txtUserName.Text
+ "' and Password = '"
+ txtPassword.Text
+ "'";
27 string strVer
= cmd.ExecuteScalar().ToString();
28 conn.Close();
29
30 //判断Verified字段是否为1如果为1说明已经激活..
31 if (String.Compare(strVer,
"1")
== 0)
32 {
33 Response.Redirect("Success.aspx");
34
35 }
36 else
37 {
38 lblShow.Visible
= true;
39 }
40
41
42 }
43 }
44 }
45
Success.aspx(成功激活后的界面) 不介绍了(就一个标签显示成功),看源代码..
Web.config中数据库连接自己修改下。
注意:发送邮件时,发现smtp.126.com,smtp.163.com不能使用,于是使用smtp.gmail.com发送邮件。
通过这个例子学到不少东西, 发送邮件,QueryString传值,解决乱码问题。。
在WIN 7 + Visual Web Develop 2008调试通过。
相关文章推荐
- (分享)ASP.NET 实现注册用户 邮箱激活
- asp.net实现一个用户进行注册的时候同时发送一个邮件到注册人的邮箱,通过发送的邮箱链接来激活该帐号
- asp.net实现一个用户进行注册的时候同时发送一个邮件到注册人的邮箱,通过发送的邮箱链接来激活该帐号
- asp.net发送邮件,使用MD5加密解密,实现用户注册完成后发送流水号(账号)到对方邮箱,jeasyUI验证,combobox的使用完成 实例下载
- asp.net 实现邮箱验证(注册的时候发送邮件激活链接的那种)
- asp.net mvc 注册中的邮箱激活功能实现
- asp.net mvc 注册中的邮箱激活功能实现
- asp.net实现邮箱激活账号
- 用户注册的邮箱激活模块的设计与实现
- ASP.NET 2.0 及MVC下实现匿名用户向注册用户的迁移(上)
- asp.net 注册用户,通过电子邮件激活
- ASP.NET jQuery 食谱12 (通过使用jQuery validation插件简单实现用户注册页面验证功能)
- 用户注册的邮箱激活模块的设计与实现
- ASP.NET注册时使用邮箱激活验证
- Asp.net 利用Jquery Ajax实现(验证用户名是否存)用户注册
- ASP.NET 2.0下实现匿名用户向注册用户的迁移(下)
- WF4.0实战(十二):ASP.NET MVC2.0结合WF4.0实现用户多步注册流程
- ASP.NET MVC通过发送邮件激活注册用户
- asp.net实现邮箱验证功能,通过邮箱链接激活该帐号