private bool CreateAgent(RegisterViewModelAgent data, string userid) { var agent = new Agent { UserId = userid, Email = data.Agent.Email, Forename = data.Agent.Forename, Lastname = data.Agent.Lastname, PostCode = data.Agent.PostCode, AddressLine = data.Agent.AddressLine, Country = data.Agent.Country, City = data.Agent.City, PhoneNumber = data.Agent.PhoneNumber, Dob = data.Agent.Dob, AgencyId = data.Agent.AgencyId }; if (AgentContext.CreateAgent(agent)) return true; return false; }
public async Task<ActionResult> RegisterAgent(RegisterViewModelAgent data) { //TODO cehck email is one sent invited to if (data.Agent.AgencyId == null) { ViewBag.Message = "You are not assigned to a valid Agency.";return View(data); } if (ModelState.IsValid) { var user = new ApplicationUser { UserName = data.Agent.Email, Email = data.Agent.Email }; var result = await UserManager.CreateAsync(user, data.Password); if (CreateAgent(data, user.Id)) { if (!Roles.RoleExists("Agent")) Roles.CreateRole("Agent"); Roles.AddUserToRole(data.Agent.Email, "Agent"); await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); // 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", "Agencies"); } AddErrors(result); } // If we got this far, something failed, redisplay form return View(data); }