Asp.Net 注册 邮箱激活

时间:2021-01-03 04:55:48

数据库 表的设计

Asp.Net 注册 邮箱激活

State为用户状态  0为禁用  1为可用  默认为0,下面有个UserGUID,这个字段将来用于激活账户

首先你要写一个表单,验证码神马的,这个我就不写了。。直接写处理的 代码在下面

          if (IsPostBack)
{
string userName = Request["UserName"];
string userPwd = Request["UserPwd"];
string userEmail = Request["UserEmail"];
string EmailGUID = Guid.NewGuid().ToString(); //UserInfosEntities
if (CheckValidCode())
{
if (InsertUserInfo(new userinfo()
{
UserName = userName,
UserPwd = userPwd,
State = ,
UserEmail = userEmail,
UserGUID = EmailGUID }))
{ string str = Request.ServerVariables["Http_Host"];      //这句话的意思是获取域名和端口,因为我是在本机调试的,要是重新生成的话端口就改了 - - 我很郁闷 ...这是大神告诉我的...
MailMessage mailMsg = new MailMessage(); //要引入System.Net这个Assembly
mailMsg.From = new MailAddress("670196906@qq.com", "自己的名字"); //源邮件地址
mailMsg.To.Add(new MailAddress(userEmail, "对方的名字")); //目的邮件地址。可以有多个收件人
mailMsg.Subject = "激活帐号"; //发送邮件的标题
userName = Common.Base64.EncodeBase64(Encoding.UTF8, userName); //这个是把传去的名字转换成base64的,我试过Encoding,不行,找了好久,中文一直乱码,只好把它转成这个样子了。。 string emailStr
                = string.Format("单击以下激活链接,激活帐号http://{0}/ActivUserInfo.aspx?UserName={1}&GUID={2}",str,userName,EmailGUID);                  //这个就是将来发到邮箱里面的激活链接
       mailMsg.Body = emailStr; //发送邮件的内容
mailMsg.IsBodyHtml = true;                      //内容是否是HTML
mailMsg.BodyEncoding = Encoding.UTF8;                //编码格式为UTF-8
SmtpClient client = new SmtpClient("smtp.qq.com"); // 发件人所使用邮箱的SMTP服务器地址。
client.Credentials = new NetworkCredential("发送邮件的帐号", "发送邮件的密码"); //发件人邮箱的用户名和密码.
client.Send(mailMsg);                        //发送邮件
Response.Redirect("/Admin.aspx"); }
else
{
Response.Redirect("/Login.aspx"); } }
else
{ Message = "验证码输入错误,请重新输入!!!";
}
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace WebDemoUserInfo
{
public partial class ActivUserInfo : System.Web.UI.Page
{
public string ActiveMessage { get; set; }
protected void Page_Load(object sender, EventArgs e)
{                                                        
string userName = Common.Base64.DecodeBase64(Encoding.UTF8, Request["UserName"]); //把传过来的UserName解密,Base64的代码在后面
string gUid = Request["GUID"];                                //
if (userName != null && gUid != null)
{
int result = CheckUserInfo(userName, gUid);
switch (result)
{
case :
ActiveMessage = "激活失败";
break;
case :
ActiveMessage = "激活成功";
break;
case :
ActiveMessage = "不能重复激活!!!";
break;
defalut: ActiveMessage = "未知错误,请联系系统管理员!!!";
}
} } private int CheckUserInfo(string userName, string gUID)
{
try
{
var db = new UserInfosEntities();
if (db.userinfo.Count(i => i.UserName == userName && i.UserGUID == gUID) == )
{
var model = db.userinfo.FirstOrDefault(i => i.UserGUID == gUID);
if (model != null && model.State == )
{
model.State = ; }
else
{
return ;
}
return db.SaveChanges() == ? : ;
}
else
{
return ;
}
}
finally
{
Dispose();
}
}
}
}
   public static class Base64
{
public static string EncodeBase64(Encoding encoding, string source)
{
byte[] bytes = encoding.GetBytes(source);
return Convert.ToBase64String(bytes);
} public static string DecodeBase64(Encoding encoding, string result)
{
byte[] bytes = Convert.FromBase64String(result);
return encoding.GetString(bytes);
}
}

Base64 加解密

整天没事自己研究...觉得还有好多要学....加油...