public async Task <IActionResult> Register(RegisterModel model) { if (ModelState.IsValid) { var user = await userManager.FindByNameAsync(model.UserName); if (user == null) { user = new StudentHousingUser { Id = Guid.NewGuid().ToString(), UserName = model.UserName, FirstName = model.FirstName, LastName = model.LastName, Email = model.Email, PhoneNumber = model.PhoneNumber }; } //if (userManager.FindByIdAsync(user.Id) != null) //{ // ModelState.AddModelError("", "Username or e-mail already registered. Try logging in."); // return View(model); //} var result = await userManager.CreateAsync(user, model.Password); if (result.Succeeded) { var token = await userManager.GenerateEmailConfirmationTokenAsync(user); var confirmationEmail = Url.Action("ConfirmEmailAddress", "Account", new { token = token, email = user.Email }, Request.Scheme); EmailConfirmation(user, confirmationEmail); ViewBag.message = "<h3>An e-mail was sent to your e-mail address with the confirmation link to register to the website. Please check your e-mail and open the link.</h3>"; ViewBag.title = "Check e-mail"; return(View("Message")); } else { foreach (var error in result.Errors) { ModelState.AddModelError("", error.Description); } return(View()); } } return(View(model)); }
private void EmailConfirmation(StudentHousingUser user, string confirmationEmail) { var message = new MimeMessage(); message.From.Add(new MailboxAddress("Student Housing", "*****@*****.**")); message.To.Add(new MailboxAddress(user.FirstName + " " + user.LastName, user.Email)); message.Subject = "E-mail Confirmation - DO NOT REPLY"; message.Body = new TextPart("plain") { Text = "Thank you for registering in the Student Housing website. Please confirm your registration by clicking on this link: " + confirmationEmail }; SendEmail(message); }