2016-03-09 80 views
0

今天我試圖按照本文的 Shai Raiten's Blog,當我完成它的createStatus返回invalidAnswer 這裏是我註冊的動作我如何在asp.net mvc的確認電子郵件發送

[HttpPost] 
    [AllowAnonymous] 
    [CaptchaValidation("CaptchaCode", "registerCaptcha", "Wrong captcha!")] 
    public ActionResult Register(RegisterModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      MembershipCreateStatus createStatus; 
      Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, false, null, out createStatus); 
      if (createStatus == MembershipCreateStatus.Success) 
      { 
       MailHelper.SendConfirmationEmail(model.UserName); 
       return RedirectToAction("Confirmation", "User"); 
      } 
      else 
      { 
       ModelState.AddModelError("", "Failed!"); 
      } 
     } 
     return View(model); 

    } 

這裏是我的RegisterModel.cs

public class RegisterModel 
{ 
    [Key] 
    public long ID { set; get; }  
    [Required(ErrorMessage = "Do not Skip this")] 
    public string UserName { set; get; } 
    [StringLength(500, MinimumLength = 6, ErrorMessage = "Atleast 6 characters in passwords")] 
    [Required(ErrorMessage = "Do not Skip this")] 
    public string Password { set; get; } 
    [Compare("Password", ErrorMessage = "Wrong confirm passwords")] 
    [Required(ErrorMessage = "Do not skip this")] 
    public string ConfirmPassword { set; get; } 
    public string Name { set; get; } 
    public string Address { set; get; } 
    [RegularExpression(@"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", ErrorMessage = "This is not an email")] 
    public string Email { set; get; } 
    public string Phone { set; get; } 
    public bool EmailConfirm { set; get; } 

} 

對我的任何建議,真的很感謝你們所做的所有幫助。

+0

你在說什麼狀態? –

回答

4

你可以做最簡單的事情是:

  • 首先,你應該定義你的用戶模型的屬性將舉行電子郵件確認標記。此外,您應該定義屬性bool IsEmailConfirmed,其默認值爲false
  • 令牌應該像自動生成的隨機字符串。例如。 Guid.NewGuid()。ToString()
  • 然後,你應該定義另一個動作,比如說[HttpGet, AllowAnonymous] ConfirmEmail(string email, string token),它將驗證該數據庫與保存在數據庫中並相應地更新IsEmailConfirmed
  • 然後你應該問的鏈接應該指向一個看起來像這樣的網址:http://YOUR.SERVER/YourController/ConfirmEmail?email={0}&token={1},其中{0}是用戶電子郵件,而{1}是你的用戶電子郵件確認令牌。它應該返回一個視圖,告訴確認是否成功。

不過,我建議不要推倒重來,並簡單地使用Asp.Net Identity 2.0 framework,這將盡一切authn & AuthZ的東西給你。

+0

您好先生,請問我再次請求您的幫助,當我卡住我最新的努力發送確認電子郵件 – pollikop

+0

當然,不客氣 – lovesan

+0

我的問題是在這個問題上,你能幫我弄清楚爲什麼** createStatus **返回invalidAnswer,如果您需要查看來自Model或Controller的更多代碼,我可以向您展示,非常感謝 – pollikop

1

請從ASP.Net站點下面的示例中,其中精美地解釋瞭如何在註冊過程中發送電子郵件。

http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-web-app-with-email-confirmation-and-password-reset

此外,我不會推薦MD5密碼hashinh因爲它的漂亮的時候,請嘗試使用SHA 256混編密碼加密。 http://forums.asp.net/t/1211478.aspx?How+do+I+use+Sha256+to+Encrypt+a+String+

+0

zaph感謝您的指點。我已經將內容編輯爲散列 – Thanigainathan

+0

MD5不再被認爲是安全的,不應該在新工作中使用,而應該使用SHA256。如果你不需要所有的32字節只需要使用所需的數字,那麼所有的字節都是隨機的。 – zaph

相關問題