public async Task <ActionResult> RegisterEmployee(RegisterCAEViewModel model, UserDetails userDetails) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { var currentUser = UserManager.FindByName(user.UserName); var roleresult = UserManager.AddToRole(currentUser.Id, "Employee"); await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); //find company with company code //set the current user id to the FK in user //set the found company's id to the foreing key property in user var company = db.Companies.Find(model.CompanyCode); // find the company by the id passed in the frontend. if (company != null) { //company.CAId = currentUser.Id; //var company = db.Companies.Find(model.CompanyCode); //set the id //UserDetails userDetails; userDetails.identtyUserId = currentUser.Id; userDetails.CompanyId = company.CompanyId; db.UsersDetails.Add(userDetails); //userDetails. = currentUser.Id; // db.Companies.Add(company); //db.SaveChanges(); //db.Entry(company).State = EntityState.Modified; db.SaveChanges(); } // For more information on how to enable account confirmation and password reset please visit http://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> RegisterCA(RegisterCAEViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { var currentUser = UserManager.FindByName(user.UserName); var roleresult = UserManager.AddToRole(currentUser.Id, "CompanyAdministrator"); await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); var company = db.Companies.Find(model.CompanyCode); if (company != null) { company.CAId = currentUser.Id; db.Entry(company).State = EntityState.Modified; db.SaveChanges(); } return(RedirectToAction("Index", "Home")); } AddErrors(result); } return(View(model)); }