pollikop
pollikop

Reputation: 97

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

Today i'm trying to follow this article of Shai Raiten's Blog and when I finish it the createStatus return invalidAnswer here is my Register action

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

    }

and here is my 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; } 

}

any suggestion for me , really appreciated all the help you guys make.

Upvotes: 1

Views: 15743

Answers (2)

Thanigainathan
Thanigainathan

Reputation: 1547

Please follow the below example from ASP.Net site where its beautifully explained how to send email during registration prcoess.

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

Additionally I would not recommend MD5 password hashinh since its pretty old, try using SHA 256 hashing for password encryption. http://forums.asp.net/t/1211478.aspx?How+do+I+use+Sha256+to+Encrypt+a+String+

Upvotes: 1

lovesan
lovesan

Reputation: 96

The simplest thing you can do is:

  • First, you should define a property in your user model which will hold email confirmation token. Also, you should define property bool IsEmailConfirmed which defaults to false.
  • The token should be something like auto-generated random string. E.g. Guid.NewGuid().ToString()
  • Then, you should define another action, say [HttpGet, AllowAnonymous] ConfirmEmail(string email, string token), which will validate that token against saved in the database and update IsEmailConfirmed accordingly.
  • And the link you are asking about, should then point to an url which will look like something like that: http://YOUR.SERVER/YourController/ConfirmEmail?email={0}&token={1}, where {0} is user email and {1} is your user email confirmation token. It should return a view that tells whether confirmation was successfull.

However, i do recommend not to reinvent the wheel and to simply use Asp.Net Identity 2.0 framework, which will do all that authn & authz stuff for you.

Upvotes: 6

Related Questions