//[ValidateAntiForgeryToken] public async Task <ActionResult> ClaimCompanyReferal([Bind(Exclude = nameof(ClaimingViewModel.IsAgreedWithTerms))] ClaimingViewModel model, string partialName) { ModelState.Remove(nameof(ClaimingViewModel.IsAgreedWithTerms)); if (ModelState.IsValid) { if (IsValidDomain(model.Company.Email, model.Email)) { var bodyHtml = System.IO.File.ReadAllText(Server.MapPath("/Templates/ClaimCompanyReferal.html")); var bodyBuilder = new StringBuilder(bodyHtml); var signupLink = $"{Request.Url.Scheme}://{Request.Url.Host}:{Request.Url.Port}{Url.Action(nameof(Details), "Business", new { id = model.Company.Id })}"; bodyBuilder.Replace("@@CompanyName@@", model.Company.Name); bodyBuilder.Replace("@@Address@@", signupLink); bodyBuilder.Replace("@@Name@@", model.Name); var emailService = new GoogleEmailService(model.Email, "Claim company", model.Email, bodyBuilder.ToString(), true, false); emailService.SendMail(); } else { await _registrationRepository.AddRegistrationRequest(new ClaimRequest { CompanyId = model.Company.Id, Email = model.Email, RequestTime = DateTime.Now }); } ViewBag.SuccessMessage = ResourceString.Instance.RegistrationSuccessfulMessage; } return(PartialView(partialName, model)); }
//[ValidateAntiForgeryToken] public async Task <ActionResult> ClaimCompany([Bind(Exclude = nameof(ClaimingViewModel.Name))] ClaimingViewModel model) { ModelState.Remove(nameof(ClaimingViewModel.Name)); if (ModelState.IsValid) { // get user var userManager = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>(); var user = await userManager.FindByNameAsync(model.Email); if (IsValidDomain(model.Company.Email, model.Email) && user == null) { //if user doesn't exist, create new one var password = $"{Guid.NewGuid().ToString().Substring(0, 8)}!1kR"; user = new ApplicationUser { UserName = model.Email, Email = model.Email, CompanyId = model.Company.Id }; // create user if (await userManager.CreateAsync(user, password) == IdentityResult.Success) { // add user role if (userManager.AddToRole(user.Id, CompanyRole) == IdentityResult.Success) { // login user await HttpContext.GetOwinContext().Get <ApplicationSignInManager>() .SignInAsync(user, isPersistent: false, rememberBrowser: false); // send password via email this.SendEmail(model, password); return(Json(new { RedirectUrl = Url.Action("BasicData", "Home", new { id = model.Company.Id }) })); } } } else { // if user exists, make a clime request await _registrationRepository.AddRegistrationRequest(new ClaimRequest { CompanyId = model.Company.Id, Email = model.Email, RequestTime = DateTime.Now }); } Session["ClaimSuccess"] = ResourceString.Instance.ClaimCompany_Success; return(Json(new { success = true })); } return(PartialView("_ClaimCompany", model)); }
/// <summary> /// Send email with new password /// </summary> private void SendEmail(ClaimingViewModel model, string password) { // send an email var bodyHtml = System.IO.File.ReadAllText(Server.MapPath("/Templates/ClaimCompany.html")); var bodyBuilder = new StringBuilder(bodyHtml); var loginLink = $"{Request.Url.Scheme}://{Request.Url.Host}:{Request.Url.Port}/{Url.Action("Details", "Business", new { id = model.Company.Id })}"; bodyBuilder.Replace("@@CompanyName@@", model.Company.Name); bodyBuilder.Replace("@@Address@@", loginLink); bodyBuilder.Replace("@@Password@@", password); var emailService = new GoogleEmailService(model.Email, "Claim company", model.Email, bodyBuilder.ToString(), true, false); emailService.SendMail(); }
public ActionResult ClaimCompany(int?id) { if (!id.HasValue) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var company = _companyRepository.GetCompanyWithDetails(id.Value); if (company == null) { return(HttpNotFound()); } var model = new ClaimingViewModel { Company = new CompanyViewModel(company) }; return(PartialView("_ClaimCompany", model)); }