2014-02-11 60 views
2

我在网站上创建一个新用户。 用户必须处于活动状态。 我必须发送一个激活电子邮件到注册的电子邮件地址与链接激活帐户。 此链接必须是已加密以便它不会被拦截。 当用户点击该链接,系统激活的账号和登录他 我user表包含:注册用户的激活邮件MVC

IDUsernamePasswordContactInfoIDAddressIDRole

ContactInformation表:

IDEmailTelCellFaxNameSurname

user.ContactInfoID是我ContactInformation.ID场。

这是我如何添加用户:

UserEmailCompare ma = new UserEmailCompare(); 
ma.email = db.ContactInformations.Where(x => x.EMail != null).Select(x => x.EMail); 

if (model.Password != model.PasswordCheck) 
{ 
    return ErrorResponse("The password you have entered does not match"); 
} 
else 
{ 
    if (ma.email.Contains(model.Email)) 
    { 
     return ErrorResponse("The e-mail address you have entered has already been registered"); 
    } 
    else 
    { 
     if (ModelState.IsValid) 
     { 
      User user; 
      ContactInformation info; 

      info = new ContactInformation() 
      { 
       EMail = model.Email 
      }; 

      db.ContactInformations.Add(info); 
      db.SaveChanges(); 

      var only = db.ContactInformations.FirstOrDefault(x => x.EMail == model.Email).ID; 
      if (model.UserID == 0) 
      { //add 
       user = new User() 
       { 
        Username = model.Username, 
        Password = Globals.CreateHashPassword(model.Password), 
        ContactInfoID = only 
       }; 

       db.Users.Add(user); 
      } 
     } 
    } 
} 

任何一个可以帮助我走得更远。 我有类发送电子邮件。

回答

1

首先,你需要一个gmail账户,与该激活邮件将发送

你喜欢的名字,您将需要进口和使用System.Net.Mail

这里是一个示例代码

const string accountName = "";        // # Gmail account name 
const string password = "";        // # Gmail account password 
MailMessage mail = new MailMessage(); 
SmtpClient smtp = new SmtpClient("smtp.gmail.com"); 

smtp.Credentails = new System.Net.NetworkCredential(accountName, password); 

mail.From = new MailAddress("[email protected]"); // # Remember to change here with the mail you got 
mail.To.Add(model.Email);         // # Email adress to send activation mail 
mail.Subject = "Activation Mail"; 
mail.Body = "Hey there, click this link for activation"; // # You will need to change here with HTML containing a link (which contains a generated activation code) 
mail.IsHtml = true; 

smtp.Send(mail); 

这是关于发送邮件给定的电子邮件地址。对于整个激活过程中,你可以按照下列步骤操作:

  • 创建包含激活链接和用户信息参数激活邮件正文一个HTML模板
  • 添加IsActiveActivationCode领域User
  • 实施激活码生成逻辑或使用Guid
  • 发送邮件成功后,更新User表设置发送激活码
  • 您需要一个激活页面,将接收生成的激活码与查询字符串和激活用户(设置IsActive为true)
+0

我已经创造了这个类。你能帮我前进吗? – Hydro

+0

@Hydro我没有包括*加密*,我认为在这个阶段或任何其他不需要。只要*加密*查询字符串值(如memberId和激活码) –

+0

'IsActive'和'ActivationCode'字段应该是什么类型的字段? – Hydro