public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Full_Name = model.FirstName, PhoneNumber = model.phone }; Customer_Business cb = new Customer_Business(); ApplicationDbContext db = new ApplicationDbContext(); var customer = new Customer(); { RolesBusiness rb = new RolesBusiness(); //rb.AddUserToRole(db.Users.FirstOrDefault(x => x.UserName == model.Email).Id, WebConstants.CustomerRole); string role = "Customer"; var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(db)); if (!roleManager.RoleExists(role)) { var urole = new IdentityRole(role); roleManager.Create(urole); } customer.FirstName = user.Full_Name; customer.LastName = "rrd"; customer.Email = user.Email; customer.phone = user.PhoneNumber; customer.address = model.address; var isSave = cb.add(customer); db.SaveChanges(); }; 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>"); return(RedirectToAction("Index", "Home")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ActionResult> RegisterCustomer(RegisterViewModel model) { RolesBusiness rb = new RolesBusiness(); Customer_Business cb = new Customer_Business(); if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Full_Name = model.FirstName + " " + model.LastName, PhoneNumber = model.phone, EmailConfirmed = true }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { try { ApplicationDbContext db = new ApplicationDbContext(); rb.AddUserToRole(db.Users.FirstOrDefault(x => x.UserName == model.Email).Id, WebConstants.CustomerRole); var customer = new Customer(); { customer.FirstName = user.Full_Name; customer.LastName = "df"; customer.Email = user.Email; customer.phone = user.PhoneNumber; customer.address = model.address; var isSave = cb.add(customer); db.SaveChanges(); }; } catch (Exception e) { ModelState.AddModelError(string.Empty, "Unable to create customer Profile."); } } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }