注册页面:
后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Net.Mail;
using System.Text;
using System.Net;
namespace 激活验证
{
public partial class region : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void sendMail(string Email,string activeCode)
{
MailMessage msg = new MailMessage();
msg.From=new MailAddress("[email protected]"); //邮件来自哪
msg.To.Add(Email);
msg.Subject = "请激活注册!";
StringBuilder contentBuilder=new StringBuilder();
contentBuilder.Append("请单击一下连接完成激活!");
contentBuilder.Append("激活");
msg.Body = contentBuilder.ToString();
msg.IsBodyHtml=true;
SmtpClient client = new SmtpClient(); //允许传输协议
client.Host = "smtp.163.com"; //发件方服务器地址
client.Port = 25; //发件方端口
NetworkCredential credential = new NetworkCredential();
credential.UserName = "[email protected]";
credential.Password = "jinyuxueqi521";
client.Credentials = credential; //说明证书要给代理证书credential
client.Send(msg);
}
protected void Button1_Click(object sender, EventArgs e)
{
string userName = TextBox1.Text.Trim();
string password = TextBox2.Text.Trim();
string Email = TextBox3.Text.Trim();
string activeCode = Guid.NewGuid().ToString().Substring(0, 8); //生成激活码
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
int number;
using (SqlConnection con=new SqlConnection(conStr))
{
string sql = "insert into T_Users (UserName,Password,Email,Active,ActiveCode) values(@username,@password,@Email,@active,@activecode)";
SqlParameter[] prams = new SqlParameter[]{
new SqlParameter("@username",userName),
new SqlParameter("@password",password),
new SqlParameter("@Email",Email),
new SqlParameter("@active",false),
new SqlParameter("@activecode",activeCode)
};
using (SqlCommand cmd=new SqlCommand(sql,con))
{
con.Open();
cmd.Parameters.AddRange(prams);
number= cmd.ExecuteNonQuery();
}
}
if (number>0)
{
sendMail( Email, activeCode);//给注册用户发邮件
Response.Redirect("regionMessage.aspx");
}
else
{
Response.Write("注册失败");
}
}
}
}
CheckActiveCode后台:
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.Configuration;
namespace 激活验证
{
public partial class CheckActiveCode : System.Web.UI.Page
{
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
int number;
protected void Page_Load(object sender, EventArgs e)
{
//去除参数id
int id = Convert.ToInt32(Request["id"]);
string activeCode = Request["ActiveCode"].ToString();
//判断id为id的记录值是否存在
//连接数据库
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "select count(*) from T_Users where id=@id";
using (SqlCommand cmd=new SqlCommand(sql,con))
{
con.Open();
cmd.Parameters.AddWithValue("@id", id);
number = Convert.ToInt32(cmd.ExecuteScalar());
}
}
if ( number > 0)
{
//如果该用户存在取出其ActiveCode字段进行比较,如果一样,把Active字段修改为true。
//连接数据库
string AC;
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "select ActiveCode from T_Users where id=@id";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.Parameters.AddWithValue("@id", id);
AC =cmd.ExecuteScalar().ToString();
}
}
if (activeCode == AC)
{
Response.Write("激活成功!");
//连接数据库
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "update T_Users set Active=1 where id=@id";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.Parameters.AddWithValue("@id", id);
number =Convert.ToInt32(cmd.ExecuteScalar());
}
}
}
else
{
Response.Write("用户存在,但是激活码错误!");
}
}
else
{
Response.Write("用户不存在!注册失败!");
}
}
}
}
regionMessage前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="regionMessage.aspx.cs" Inherits="激活验证.regionMessage" %>