2014-02-11 135 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

相關問題