public async Task <ActionResult> RegisterOwner(RegisterOwnerViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email, UserType = "Owner" }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); UserManager.AddToRole(user.Id, "Owner"); Owner owner = new Owner() { FirstName = model.FirstName, LastName = model.LastName, Address = model.Address, PhoneNumber = model.PhoneNumber, ApplicationUserId = user.Id, //other attributes as well }; ApplicationDbContext db = new ApplicationDbContext(); db.owner.Add(owner); Console.WriteLine(owner); db.SaveChanges(); return(RedirectToAction("Index", "Home", null)); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ActionResult> RegisterOwner(RegisterOwnerViewModel model) { if (ModelState.IsValid) { //Checking the register code in the db and delete the code if exists if (OwnerRegisterCodeManager.CheckOwnerRegisterCode(model.RegisterCode)) { var owner = new OwnerUsers { UserName = model.UserName.Trim(' '), Email = model.Email }; var result = await UserManager.CreateAsync(owner, model.Password); if (result.Succeeded) { //Send notification to office mail await EmailNotifications.RegiteredUserNotification(owner.UserName, owner.Email, owner.PhoneNumber); var currentUser = await UserManager.FindByNameAsync(owner.UserName); await UserManager.AddToRoleAsync(currentUser.Id, "Owner"); // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link string code = await UserManager.GenerateEmailConfirmationTokenAsync(owner.Id); if (Request.Url != null) { var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = owner.Id, code }, Request.Url.Scheme); await EmailNotifications.NoReplyMailService.SendHtmlEmailAsync(owner.Email, "Потвърдете акаунтът си в sProperties", "За да потвърдите регистрацията си натиснете <a href=\"" + callbackUrl + "\">ТУК</a>"); } return(RedirectToAction("Login", "Account", new { confirm = "email" })); } AddErrorsToModelState(result); } else { ModelState.AddModelError("RegisterCode", "Invalid Register Code"); return(View()); } } return(View()); }