public async Task <IActionResult> Index()
        {
            CheckOutViewModel model = new CheckOutViewModel();

            await GetCurrentCart(model);

            if (User.Identity.IsAuthenticated)
            {
                TCPUser currentUser = await _signInManager.UserManager.GetUserAsync(User);

                Braintree.CustomerSearchRequest search = new Braintree.CustomerSearchRequest();
                search.Email.Is(currentUser.Email);
                var searchResult = await _brainTreeGateway.Customer.SearchAsync(search);

                if (searchResult.Ids.Count > 0)
                {
                    Braintree.Customer customer = searchResult.FirstItem;
                    model.CreditCards = customer.CreditCards;
                    model.Addresses   = customer.Addresses;
                }
            }
            if (model.Cart == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            return(View(model));
        }
        public async Task <IActionResult> SignIn(SignInViewModel model)
        {
            if (ModelState.IsValid)
            {
                TCPUser existingUser = await _signInManager.UserManager.FindByEmailAsync(model.email);

                if (existingUser != null)
                {
                    Microsoft.AspNetCore.Identity.SignInResult passwordResult = await this._signInManager.CheckPasswordSignInAsync(existingUser, model.password, false);

                    if (passwordResult.Succeeded)
                    {
                        await this._signInManager.SignInAsync(existingUser, false);

                        return(RedirectToAction("Index", "Home"));
                    }
                    else
                    {
                        ModelState.AddModelError("PasswordIncorrect", "Username or Password is incorrect.");
                    }
                }
                else
                {
                    ModelState.AddModelError("UserDoesNotExist", "Username or Password is incorrect.");
                }
            }
            return(View());
        }
Exemple #3
0
 void OnApplicationQuit()
 {
     if (m_ServerSession != null)
     {
         m_ServerSession.Close();
         m_ServerSession = null;
     }
     if (m_ClientSession != null)
     {
         m_ClientSession.Close();
         m_ClientSession = null;
     }
 }
        public async Task <IActionResult> Register(Models.RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                TCPUser newUser = new TCPUser {
                    Email       = model.email,
                    UserName    = model.userName,
                    FirstName   = model.firstName,
                    LastName    = model.lastName,
                    PhoneNumber = model.phoneNumber
                };

                IdentityResult creationResult = this._signInManager.UserManager.CreateAsync(newUser).Result;
                if (creationResult.Succeeded)
                {
                    IdentityResult passwordResult = this._signInManager.UserManager.AddPasswordAsync(newUser, model.password).Result;
                    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 The Chesed Project!",
                            "<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 creationResult.Errors)
                        {
                            ModelState.AddModelError(error.Code, error.Description);
                        }
                    }
                }
                else
                {
                    foreach (var error in creationResult.Errors)
                    {
                        ModelState.AddModelError(error.Code, error.Description);
                    }
                }
            }
            return(View());
        }