簡體   English   中英

我如何在asp.net mvc中發送確認電子郵件

[英]How can I send a Confirmation Email in asp.net mvc

今天,我試圖遵循Shai Raiten博客中的這篇文章,當我完成本文時,createStatus返回invalidAnswer,這是我的Register操作

[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; } 

}

對我的任何建議,非常感謝你們提供的所有幫助。

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

  • 首先,您應該在用戶模型中定義一個屬性,該屬性將保存電子郵件確認令牌。 另外,您應該定義屬性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框架 ,它將為您完成所有authn和authz的工作。

請遵循以下來自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+

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM