public async Task <IActionResult> SignUp(SignUpVM newUser) { if (!ModelState.IsValid) { return(View(newUser)); } var checkEmail = new EmailAddressAttribute(); if (!checkEmail.IsValid(newUser.Email)) { ModelState.AddModelError("Email", "This email is invalid"); return(View(newUser)); } try { var checkEmail2 = new System.Net.Mail.MailAddress(newUser.Email); if (checkEmail2.Address != newUser.Email) { ModelState.AddModelError("Email", "This email is invalid"); return(View(newUser)); } } catch { ModelState.AddModelError("Email", "This email is invalid"); return(View(newUser)); } if (DateTime.Today.Year - newUser.BirthDate.Year <= 1) { ModelState.AddModelError("Birthday", "The user must be older than 1 year"); return(View(newUser)); } User user = new User { Firstname = newUser.Firtsname.Trim(), Lastname = newUser.Lastname.Trim(), Email = newUser.Email.Trim(), BirthDate = newUser.BirthDate, UserName = newUser.Firtsname.Trim() + newUser.Lastname.Trim() + Guid.NewGuid().ToString() }; switch (newUser.GenderId) { case 1: user.GenderId = 1; user.Avatar = "default1.jpg"; break; case 2: user.GenderId = 2; user.Avatar = "default2.jpg"; break; default: ModelState.AddModelError("GenderId", "Select valid gender"); return(View(newUser)); } IdentityResult identityResult = await userManager.CreateAsync(user, newUser.Password); if (!identityResult.Succeeded) { foreach (var er in identityResult.Errors) { ModelState.AddModelError("", er.Description); } return(View(newUser)); } await userManager.AddToRoleAsync(user, Utilities.SD.MemberRole); await userManager.UpdateAsync(user); string emailConfirmationToken = await userManager.GenerateEmailConfirmationTokenAsync(user); #region Sending Email Confirmation Message SmtpClient client = new SmtpClient("smtp.gmail.com", 587); client.UseDefaultCredentials = false; client.EnableSsl = true; client.Credentials = new NetworkCredential(configuration["ConnectionStrings:SmtpClientCredentialEmail"], configuration["ConnectionStrings:SmtpClientCredentialPassword"]); MailMessage message = new MailMessage(configuration["ConnectionStrings:SmtpClientCredentialEmail"], newUser.Email); message.IsBodyHtml = true; message.Subject = "Confirm account"; message.Body = $"<table style='width:100%;background-color:#fbfbfb;padding:50px'><thead style ='width:100%;display:flex;justify-content:center;'><tr style ='width:100%;display:flex;justify-content:center;'><th style ='width:100%;color:#7e0f9a;font-family:Roboto, sans-serif;font-weight:400;font-size:50px'>Family Tree</th></tr><thead><tbody><tr><td style ='padding:30px 0px;color:#353535;font-family:Roboto Condensed, sans-serif;font-size:20px;'> Dear user, you have successfully signed up. Click the 'Verify Account' button below to verify your account.</td></tr><tr><td style ='font-family:Roboto Condensed, sans-serif;text-align:center;'><a href='https://localhost:44341/account/confirmemail?id={user.Id}&emailConfirmationToken={emailConfirmationToken}' style ='text-decoration:none;padding:10px 30px;border-radius:3px;background-color:#8d11ff;color:black;font-weight:lighter;font-size:20px;cursor:pointer;'>Confirm account</a></td></tr></tbody></table>"; client.Send(message); #endregion TempData["signup"] = true; await signInManager.SignInAsync(user, true); Family family = new Family { Name = $"{user.Firstname}'s family", Logo = "defaultfamily.png" }; db.Families.Add(family); db.SaveChanges(); FamilyToUser familyToUser = new FamilyToUser { FamilyId = family.Id, UserId = user.Id }; await db.FamilyToUsers.AddAsync(familyToUser); db.SaveChanges(); Person person = new Person { Age = DateTime.Today.Year - user.BirthDate.Year, Birthdate = user.BirthDate, FamilyId = family.Id, Firstname = user.Firstname, GenderId = user.GenderId, LastName = user.Lastname, Photo = user.Avatar }; db.People.Add(person); db.SaveChanges(); UserToPerson userToPerson = new UserToPerson { PersonId = person.Id, UserId = user.Id }; db.UserToPeople.Add(userToPerson); db.SaveChanges(); Relationship relationship = new Relationship { IsMain = true, FamilyId = family.Id, RelatedUserId = person.Id, RelRoleId = db.RelRoles.FirstOrDefault(x => x.Name.ToLower() == "norole").Id }; db.Relationships.Add(relationship); db.SaveChanges(); return(RedirectToAction("Index", "Home")); }
public async Task <IActionResult> Confirminvate(ConfirmInvateVM vm) { if (!ModelState.IsValid) { return(View(vm)); } PersonToken personToken = db.PersonTokens.FirstOrDefault(x => x.Code == vm.Token); if (personToken == null) { return(NotFound()); } if (personToken.Date.AddDays(1) < DateTime.Now) { return(NotFound()); } User user = await userManager.FindByIdAsync(personToken.UserId); if (user == null) { return(NotFound()); } User invateUser = await userManager.FindByEmailAsync(personToken.Email); if (invateUser != null) { return(NotFound()); } Person person = db.People.Include(x => x.UserToPerson).FirstOrDefault(x => x.Id == vm.Id && x.FamilyId == vm.FamilyId); if (person == null) { return(NotFound()); } if (person.UserToPerson != null) { return(NotFound()); } int familId = FamlyMethods.GetFamilyId(db, user); if (familId != vm.FamilyId) { return(NotFound()); } User newUser = new User { Firstname = person.Firstname, Lastname = person.LastName, Email = personToken.Email, BirthDate = person.Birthdate, UserName = person.Firstname.Trim() + person.LastName.Trim() + Guid.NewGuid().ToString(), EmailConfirmed = true, Avatar = person.Photo }; switch (person.GenderId) { case 1: newUser.GenderId = 1; newUser.Avatar = "default1.jpg"; break; case 2: newUser.GenderId = 2; newUser.Avatar = "default2.jpg"; break; } IdentityResult identityResult = await userManager.CreateAsync(newUser, vm.Password); if (!identityResult.Succeeded) { foreach (var er in identityResult.Errors) { ModelState.AddModelError("", er.Description); } return(View(vm)); } await userManager.AddToRoleAsync(newUser, Utilities.SD.MemberRole); await userManager.UpdateAsync(newUser); await signInManager.SignInAsync(newUser, true); FamilyToUser familyToUser = new FamilyToUser { FamilyId = vm.FamilyId, UserId = newUser.Id }; await db.FamilyToUsers.AddAsync(familyToUser); db.SaveChanges(); UserToPerson userToPerson = new UserToPerson { PersonId = person.Id, UserId = newUser.Id }; db.UserToPeople.Add(userToPerson); db.PersonTokens.Remove(personToken); db.SaveChanges(); return(RedirectToAction("Index", "Home")); }