public async Task <ActionResult> AddAgent(AddAgentViewModel model) { if (ModelState.IsValid) { var applicationUser = new ApplicationUser { Email = model.Email, UserName = model.Email, FirstName = model.FirstName, LastName = model.LastName, }; var result = await _authManager.CreateAsync(applicationUser, model.Password); if (result.Succeeded) { var newUser = await _authManager.FindByNameAsync(model.Email); await _agencyService.CreateAgentAsync(User.Identity.GetUserId(), newUser, model); #region Send confirmation Email to agent string code = await _authManager.GenerateEmailConfirmationTokenAsync(newUser.Id); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = newUser.Id, code = code, area = "" }, protocol: Request.Url.Scheme); var emailModel = new CreateAgentMailViewModel(); emailModel.Name = model.FirstName + " " + model.LastName; emailModel.ConfirmationUrl = callbackUrl; emailModel.Password = model.Password; emailModel.Email = model.Email; var mailContent = EmailSender.GetRazorViewAsString(emailModel, "~/Views/EmailTemplates/CreateAgent.cshtml"); try { await _authManager.SendEmailAsync(userId : newUser.Id, subject : Resource.Email_Register_Mail_Title, body : mailContent); } catch (Exception) { this.AddToastMessage(Resource.Error, Resource.An_error_occurred_while_processing_your_request, ToastType.Error); } #endregion var roleResult = await _authManager.AddUserToRoleAsync(newUser.Id, RoleDefinitions.Agent.ToString()); if (!roleResult.Succeeded) { roleResult.Errors.ForEach(c => ModelState.AddModelError("", c)); } return(RedirectToAction("Step3", "Agency", new { area = "Agent" })); } result.Errors.ForEach(c => ModelState.AddModelError("", c)); } return(View(model)); }
public async Task <ActionResult> Register(AccountModels model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.RegisterViewModel.Email, FirstName = model.RegisterViewModel.FirstName, LastName = model.RegisterViewModel.LastName, Email = model.RegisterViewModel.Email }; var result = await _authManager.CreateAsync(user, model.RegisterViewModel.Password); if (result.Succeeded) { await _authManager.AddUserToRoleAsync(user.Id, RoleDefinitions.SuperAgent.ToString()); string code = await _authManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); var emailModel = new RegistrationMailViewModel(); emailModel.Name = model.RegisterViewModel.FirstName + " " + model.RegisterViewModel.LastName; emailModel.ConfirmationUrl = callbackUrl; var mailContent = EmailSender.GetRazorViewAsString(emailModel, "~/Views/EmailTemplates/Register.cshtml"); await _authManager.SendEmailAsync(userId : user.Id, subject : Resource.Email_Register_Mail_Title, body : mailContent); var detailedSearchVm = Session["SearchViewModel"] as DetailedSearchResultsModel; if (detailedSearchVm != null) { _searchProfileService.SaveDetailedSearchCriterias(detailedSearchVm, user.Id); this.AddToastMessage(Resource.Success, Resource.Please_Check_Your_Email_Account_To_Confirm_Your_Email_Address, ToastType.Success); return(RedirectToAction("DetailedSearch", "Home")); } return(View("RegisterConfirmation")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }