public async Task <IActionResult> LogIn(LoginViewModel model) { if (ModelState.IsValid) { BoatChartesUser existingUser = await this._signInManager.UserManager.FindByNameAsync(model.UserName); if (existingUser != null) { Microsoft.AspNetCore.Identity.SignInResult passwordResult = await this._signInManager.CheckPasswordSignInAsync(existingUser, model.Password, false); if (passwordResult.Succeeded) { this._signInManager.SignInAsync(existingUser, false).Wait(); return(RedirectToAction("Index", "Home")); } else { ModelState.AddModelError("PasswordIncorrect", "Username or Password is incorrect."); } } else { ModelState.AddModelError("UserDoesNotExist", "Username or Password is incorrect."); } } return(View()); }
public async Task <IActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { BoatChartesUser newUser = new BoatChartesUser { UserName = model.UserName, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, PhoneNumber = model.PhoneNumber }; IdentityResult creationResult = await this._signInManager.UserManager.CreateAsync(newUser); if (creationResult.Succeeded) { IdentityResult passwordResult = await this._signInManager.UserManager.AddPasswordAsync(newUser, model.Password); if (passwordResult.Succeeded) { Braintree.CustomerSearchRequest search = new Braintree.CustomerSearchRequest(); search.Email.Is(model.Email); var searchResult = await _braintreeGateway.Customer.SearchAsync(search); if (searchResult.Ids.Count == 0) { //create a new Braintree customer await _braintreeGateway.Customer.CreateAsync(new Braintree.CustomerRequest { Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, Phone = model.PhoneNumber }); } else { //update the existing Braintree customer Braintree.Customer existingCustomer = searchResult.FirstItem; await _braintreeGateway.Customer.UpdateAsync(existingCustomer.Id, new Braintree.CustomerRequest { FirstName = model.FirstName, LastName = model.LastName, Phone = model.PhoneNumber }); } var confirmationToken = await _signInManager.UserManager.GenerateEmailConfirmationTokenAsync(newUser); confirmationToken = System.Net.WebUtility.UrlEncode(confirmationToken); string currentUrl = Request.GetDisplayUrl(); //This will get me the URL for the current request System.Uri uri = new Uri(currentUrl); //This will wrap it in a "URI" object so I can split it into parts string confirmationUrl = uri.GetLeftPart(UriPartial.Authority); //This gives me just the scheme + authority of the URI confirmationUrl += "/account/confirm?id=" + confirmationToken + "&userId=" + System.Net.WebUtility.UrlEncode(newUser.Id); await this._signInManager.SignInAsync(newUser, false); var emailResult = await this._emailService.SendEmailAsync( model.Email, "Welcome to My Boat!", "<p>Thanks for signing up, " + model.UserName + "!</p><p><a href=\"" + confirmationUrl + "\">Confirm your account<a></p>", "Thanks for signing up, " + model.UserName + "!" ); if (!emailResult.Success) { throw new Exception(string.Join(',', emailResult.Errors.Select(x => x.Message))); } return(RedirectToAction("Index", "Home")); } else { foreach (var error in passwordResult.Errors) { ModelState.AddModelError(error.Code, error.Description); } } } else { foreach (var error in creationResult.Errors) { ModelState.AddModelError(error.Code, error.Description); } } } return(View()); }