public ActionResult RegisterMerchant() { ViewBag.SiteKey = WebConfigurationManager.AppSettings["reCAPTCHASiteKey"]; var viewModel = new RegisterMerchantViewModel { }; return(View(viewModel)); }
public async Task <IActionResult> RegisterMerchant(RegisterMerchantViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ActionResult> RegisterMerchant(RegisterMerchantViewModel model) { //Validate Google recaptcha here. //get true if recaptcha was successful var status = VerifyReCaptcha(); if (!status) { ModelState.AddModelError("", "Google reCaptcha validation failed"); } if (ModelState.IsValid && status) { var user = new ApplicationUser { UserName = model.Email.Trim(), Email = model.Email.Trim(), PhoneNumber = model.PhoneNumber.Trim(), FirstName = model.FirstName.Trim(), LastName = model.LastName.Trim(), }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { try { //1. Assign the user to a merchant var merchant = new Merchant() { MerchantID = user.Id, CompanyDescriptiton = model.CompanyDescription.Trim(), CompanyName = model.CompanyName.Trim(), CompanyWebsite = model.CompanyWebsite.Trim() }; //Initialize addresses merchant.Addresses = new List <MerchantAddress>(); merchant.Addresses.Add(new MerchantAddress { City = model.City.Trim(), Country = model.Country.Trim(), Parish = model.Parish.Trim(), StreetAddress1 = model.StreetAddress1.Trim(), StreetAddress2 = model.StreetAddress2.Trim() }); _context.Merchants.Add(merchant); _context.SaveChanges(); } catch (Exception ex) { throw; } //1.Get UserRoles //2.If Role is not there create //3.Assigns Customer to Customer Role //Assign Customer to user var getRole = _context.Roles.SingleOrDefault(c => c.Name.Contains(RoleName.Merchant)); if (getRole == null) { //Temp code to create rolls and add user var roleStore = new RoleStore <IdentityRole>(new ApplicationDbContext()); //roll manager var roleManager = new RoleManager <IdentityRole>(roleStore); //to create the role await roleManager.CreateAsync(new IdentityRole(RoleName.Merchant)); //Add User to the Role await UserManager.AddToRoleAsync(user.Id, RoleName.Merchant); } else { //Add User to the Role await UserManager.AddToRoleAsync(user.Id, RoleName.Merchant); } #region Create and Add Roles //Temp code to create rolls and add user //var roleStore = new RoleStore<IdentityRole>(new ApplicationDbContext()); ////roll manager //var roleManager = new RoleManager<IdentityRole>(roleStore); ////to create the role //await roleManager.CreateAsync(new IdentityRole(RoleName.Admin)); ////Add User to the Role //await UserManager.AddToRoleAsync(user.Id,RoleName.Admin); #endregion //Change this code to send confirmation email to users verus logging them in immediately //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>"); await UserManager.SendEmailAsync(user.Id, EmailSubject.ConfirmEmail, callbackUrl); return(RedirectToAction("Index", "Home")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }