版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!
恰饭广告
实现效果:(图片较大,加载较慢)
注意:
1.为了更快的查看效果,链接失效时间设置了1分钟
2.请正确填写发件人邮件和密码或者授权码,关于邮箱开启第三方登录(开通POP)不在赘述
3.大致流程:注册用户后state=0, acticode为用户名+当前时间(毫秒级)并加密再写入数据库,注册成功state=1,过期时间修改为永久,链接失效则会删除用户,如对激活链接进行修改则会提示无效链接
4.彪悍的代码不需要注释…(懒癌)
数据库表:
项目的数据模块图:
BLL层:
using EmailRegLink.DAL; using EmailRegLink.Model; using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace EmailRegLink.BLL { public class UserInfoBLL { UserInfoDAL userDal = new UserInfoDAL(); public void Insert(UserInfo user) { userDal.Insert(user); } public void update(int id) { userDal.update(id); } public UserInfo QueryId(string UserName) { return userDal.QueryId(UserName); } public void DeleteById(int id) { userDal.DeleteById(id); } public UserInfo isUser(string userName, string Email) { return userDal.isUser(userName,Email); } } }
DAL层:
using EmailRegLink.Model; using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Web; namespace EmailRegLink.DAL { public class UserInfoDAL { SQLHelper sqlHelp = new SQLHelper(); public void Insert(UserInfo user) { sqlHelp.ExecuteNonQuery(@"INSERT INTO regUser (userName, password,Email,state,actiCode,expTime) VALUES (@userName, @password,@Email,@state,@actiCode,@expTime)", new SqlParameter("@userName", user.userName), new SqlParameter("@password", user.password), new SqlParameter("@Email",user.email), new SqlParameter("@state",user.state=0), new SqlParameter("@actiCode", user.actiCode), new SqlParameter("@expTime", user.expTime)); } public void update(int id) { sqlHelp.ExecuteNonQuery(@"update regUser set state=1,expTime='9999/12/31 00:00:00' where id=@id", new SqlParameter("@id", id)); } public UserInfo QueryId(string userName) { UserInfo user = new UserInfo(); SqlDataReader reader = sqlHelp.ExecuteSqlReader("select id,userName,Email,actiCode,expTime,state from regUser where userName=@userName", new SqlParameter("@userName", userName)); while (reader.Read()) { user.id = reader.GetInt32(0); user.userName = reader.GetString(1); user.email = reader.GetString(2); user.actiCode = reader.GetString(3); user.expTime = reader.GetDateTime(4); user.state = reader.GetInt32(5); } reader.Close(); return user; } public void DeleteById(int id) { sqlHelp.ExecuteNonQuery("delete from regUser where id=@id", new SqlParameter("@id", id)); } public UserInfo isUser(string userName,string Email) { UserInfo user = new UserInfo(); SqlDataReader reader = sqlHelp.ExecuteSqlReader("select userName,Email from regUser where userName=@userName or Email=@Email", new SqlParameter("@userName", userName), new SqlParameter("@Email", Email)); while (reader.Read()) { user.userName = reader.GetString(0); user.email = reader.GetString(1); } reader.Close(); return user; } } }
Model层(Entity类):
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace EmailRegLink.Model { public class UserInfo { public int id { get; set; } public string userName { get; set; } public string password { get; set; } public string email { get; set; } public int state { get; set; } public string actiCode { get; set; } public DateTime expTime { get; set; } } }
UI层:
Regist.aspx.cs代码
using EmailRegLink.BLL; using EmailRegLink.Model; using System; using System.Collections.Generic; using System.Linq; using System.Net.Mail; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace EmailRegLink { public partial class Regist : System.Web.UI.Page { UserInfo user = new UserInfo(); UserInfoBLL userBll = new UserInfoBLL(); protected void Page_Load(object sender, EventArgs e) { } private bool isUser() { string userName = txtName.Text.Trim(); string mail = txtMail.Text.Trim(); string password = txtPwd.Text.Trim(); user = userBll.isUser(userName, mail); if (userName=="") { Response.Write("用户名不能为空"); return false; } if (password == "") { Response.Write("密码不能为空"); return false; } if (mail == "") { Response.Write("邮箱不能为空"); return false; } if (userName == user.userName) { Response.Write("用户名已存在"); return false; } if (mail == user.email) { Response.Write("邮箱已存在"); return false; } else { userName = ""; mail = ""; password = ""; return true; } } protected void btnReg_Click(object sender, EventArgs e) { if (isUser() == true) { user.userName = txtName.Text.Trim(); user.password = txtPwd.Text.Trim(); user.email = txtMail.Text.Trim(); DateTime now = DateTime.Now; string strTime = DateTime.Now.AddMinutes(1).ToString() + "." + now.Millisecond; DateTime dt = Convert.ToDateTime(strTime); user.expTime = dt; string strCode = user.userName + dt; user.actiCode = EnDecrypt.Encrypt(strCode); userBll.Insert(user); sendMail(); Response.Write("邮件已发送"); } } private void sendMail() { string addresser = "邮箱账号"; string recipient = txtMail.Text.Trim(); string userName = txtName.Text.Trim(); string emailPwd = "邮件密码或者授权码"; user = userBll.QueryId(userName); string id = EnDecrypt.Encrypt(user.id.ToString()); string code = user.actiCode; string title = "感谢您注册,请验证邮箱(邮箱注册)"; string str = string.Format("http://localhost:9312/RegistSuccess.aspx?userName={0}&id={1}&token={2}", userName, id, code); //激活码链接 string content = "请点击下面的链接完成邮箱验证 " + str; MailMessage message = new MailMessage(addresser, recipient); message.Subject = title; message.Body = content; message.Priority = MailPriority.High; SmtpClient client = new SmtpClient("邮箱服务器地址", 25); client.EnableSsl = false; client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential(addresser, emailPwd); client.Send(message); } } }
RegistSuccess.aspx.cs代码
using EmailRegLink.BLL; using EmailRegLink.Model; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace EmailRegLink { public partial class RegistSuccess : System.Web.UI.Page { UserInfo user = new UserInfo(); UserInfoBLL userBll = new UserInfoBLL(); protected void Page_Load(object sender, EventArgs e) { DateTime now = DateTime.Now; string strTime = DateTime.Now.ToString() + "." + now.Millisecond; DateTime dt = Convert.ToDateTime(strTime); string userName = Request.QueryString["userName"]; string id =Request.QueryString["id"]; string token = Request.QueryString["token"]; string url = Request.Url.AbsoluteUri.ToString(); if (url=="http://localhost:9312/RegistSuccess.aspx" || userName == null || id == null || token == null) { Response.Write("无效链接"); return; } if (!IsPostBack) { user = userBll.QueryId(userName); string name = user.userName; DateTime exptime = user.expTime; string code = user.actiCode; int flag = user.state; if (name == userName && flag == 0 && dt < exptime) { userBll.update(Convert.ToInt32(EnDecrypt.Decrypt(id))); Response.Write("注册成功"); } else { if (code == token && name == userName && flag == 1) { Response.Write("用户已激活"); return; } if (name == userName && flag == 0 && dt > exptime) { userBll.DeleteById(Convert.ToInt32(EnDecrypt.Decrypt(id))); Response.Write("失效,请重新注册"); return; } else { Response.Write("无效链接"); } } } } } }
加解密类:(来源https://www.cnblogs.com/wifi/articles/2482350.html)
还有数据库访问类(SQLHelper.cs)和url加解密类(EnDecrypt.cs)就不贴出来了
GitHub下载地址:https://github.com/kiritobin/EmailRegLink
原文链接:https://www.idaobin.com/archives/1285.html
让我恰个饭吧.ヘ( ̄ω ̄ヘ)
恰饭广告