1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid) //TODO Change this to use a worker to send emails.
{
// Check if email exists already before creating new user
using (UsersContext db = new UsersContext())
{
UserProfile email = db.UserProfiles.FirstOrDefault(u => u.Email.ToLower() == model.Email.ToLower());
UserProfile uName =
db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
// Attempt to register the user
try
{
if (email == null && uName == null && this.IsCaptchaVerify("Captcha is not valid"))
{
bool requireEmailConfirmation = !WebMail.SmtpServer.IsEmpty();
string confirmationToken = WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new
{
FirstName = model.FirstName,
LastName = model.LastName,
Email = model.Email
},
requireEmailConfirmation);
if (requireEmailConfirmation)
{
EmailViewModel eml = new EmailViewModel
{
ToEmail = model.Email,
Subject = "Confirmez votre inscription",
FirstName = model.FirstName,
LastName = model.LastName,
Body = confirmationToken
};
UserMailer.ConfirmRegistration(eml).SendAsync();
Response.Redirect("~/Account/Thanks");
}
else
{
WebSecurity.Login(model.UserName, model.Password);
Response.Redirect("~/");
}
}
else
{
if (email != null)
ModelState.AddModelError("Email", "Email address already exists. Please enter a different email address.");
if (uName != null)
ModelState.AddModelError("UserName", "User Name already exists. Please enter a different user name.");
if (!this.IsCaptchaVerify("Captcha is not valid"))
TempData["ErrorMessage"] = "Captcha is not valid";
}
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
}
// If we got this far, something failed, redisplay form
return View(model);
} |
Partager