首页 > 代码库 > Asp.Net 注册 邮箱激活
Asp.Net 注册 邮箱激活
数据库 表的设计
State为用户状态 0为禁用 1为可用 默认为0,下面有个UserGUID,这个字段将来用于激活账户
首先你要写一个表单,验证码神马的,这个我就不写了。。直接写处理的 代码在下面
if (IsPostBack) 2 { 3 string userName = Request["UserName"]; 4 string userPwd = Request["UserPwd"]; 5 string userEmail = Request["UserEmail"]; 6 string EmailGUID = Guid.NewGuid().ToString(); 7 8 //UserInfosEntities 9 if (CheckValidCode())10 {11 if (InsertUserInfo(new userinfo()12 {13 UserName = userName,14 UserPwd = userPwd,15 State = 0,16 UserEmail = userEmail,17 UserGUID = EmailGUID18 19 }))20 {21 22 string str = Request.ServerVariables["Http_Host"]; //这句话的意思是获取域名和端口,因为我是在本机调试的,要是重新生成的话端口就改了 - - 我很郁闷 ...这是大神告诉我的...23 MailMessage mailMsg = new MailMessage(); //要引入System.Net这个Assembly24 mailMsg.From = new MailAddress("670196906@qq.com", "自己的名字"); //源邮件地址 25 mailMsg.To.Add(new MailAddress(userEmail, "对方的名字")); //目的邮件地址。可以有多个收件人26 mailMsg.Subject = "激活帐号"; //发送邮件的标题 27 userName = Common.Base64.EncodeBase64(Encoding.UTF8, userName); //这个是把传去的名字转换成base64的,我试过Encoding,不行,找了好久,中文一直乱码,只好把它转成这个样子了。。 28 string emailStr = string.Format("单击以下激活链接,激活帐号http://{0}/ActivUserInfo.aspx?UserName={1}&GUID={2}",str,userName,EmailGUID); //这个就是将来发到邮箱里面的激活链接 29 mailMsg.Body = emailStr; //发送邮件的内容 30 mailMsg.IsBodyHtml = true; //内容是否是HTML31 mailMsg.BodyEncoding = Encoding.UTF8; //编码格式为UTF-832 SmtpClient client = new SmtpClient("smtp.qq.com"); // 发件人所使用邮箱的SMTP服务器地址。 33 client.Credentials = new NetworkCredential("发送邮件的帐号", "发送邮件的密码"); //发件人邮箱的用户名和密码.34 client.Send(mailMsg); //发送邮件35 Response.Redirect("/Admin.aspx");36 37 }38 else39 {40 Response.Redirect("/Login.aspx");41 42 }43 44 45 }46 else47 {48 49 Message = "验证码输入错误,请重新输入!!!";50 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Web; 6 using System.Web.UI; 7 using System.Web.UI.WebControls; 8 9 namespace WebDemoUserInfo10 {11 public partial class ActivUserInfo : System.Web.UI.Page12 {13 public string ActiveMessage { get; set; }14 protected void Page_Load(object sender, EventArgs e)15 { 16 string userName = Common.Base64.DecodeBase64(Encoding.UTF8, Request["UserName"]); //把传过来的UserName解密,Base64的代码在后面17 string gUid = Request["GUID"]; //18 if (userName != null && gUid != null)19 {20 int result = CheckUserInfo(userName, gUid);21 switch (result)22 {23 case 0:24 ActiveMessage = "激活失败";25 break;26 case 1:27 ActiveMessage = "激活成功";28 break;29 case 2:30 ActiveMessage = "不能重复激活!!!";31 break;32 defalut: ActiveMessage = "未知错误,请联系系统管理员!!!";33 }34 }35 36 37 }38 39 private int CheckUserInfo(string userName, string gUID)40 {41 try42 {43 var db = new UserInfosEntities();44 if (db.userinfo.Count(i => i.UserName == userName && i.UserGUID == gUID) == 1)45 {46 var model = db.userinfo.FirstOrDefault(i => i.UserGUID == gUID);47 if (model != null && model.State == 0)48 {49 model.State = 1;50 51 }52 else53 {54 return 2;55 }56 return db.SaveChanges() == 1 ? 1 : 0;57 }58 else59 {60 return 0;61 }62 }63 finally64 {65 Dispose();66 }67 }68 }69 }
1 public static class Base64 2 { 3 public static string EncodeBase64(Encoding encoding, string source) 4 { 5 byte[] bytes = encoding.GetBytes(source); 6 return Convert.ToBase64String(bytes); 7 } 8 9 public static string DecodeBase64(Encoding encoding, string result)10 {11 byte[] bytes = Convert.FromBase64String(result);12 return encoding.GetString(bytes);13 }14 }
整天没事自己研究...觉得还有好多要学....加油...
Asp.Net 注册 邮箱激活
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。