コード例 #1
0
        public async Task <IActionResult> SignIn(string email, string password)
        {
            if (ModelState.IsValid)
            {
                // IdentityUser existingUser = _signInManager.UserManager.FindByNameAsync(email).Result;
                OrganicStoreUser existingUser = await _signInManager.UserManager.FindByNameAsync(email);

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

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

                        return(RedirectToAction("Index", "Product"));
                    }
                    else
                    {
                        ModelState.AddModelError("password", " Wrong username or password");
                    }
                }
                else
                {
                    ModelState.AddModelError("username", "Wrong username or password");
                }
            }
            return(View());
        }
コード例 #2
0
        public async Task <IActionResult> Index()
        {
            Checkout model = new Checkout();

            await GetCurrentCart(model);

            if (User.Identity.IsAuthenticated)
            {
                OrganicStoreUser 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));
        }
コード例 #3
0
        [ValidateAntiForgeryToken]                                          //Demands the right token from the submitted page by making sure original user heuristics are the same
        public async Task <IActionResult> Register(RegisterViewModel model) //We're binding the RegisterViewModel model class to access it's properties
        {
            if (ModelState.IsValid)
            {
                //TODO: Create an account and log him in
                //  OrganicStoreUser newUser = new OrganicStoreUser(model.UserName);
                OrganicStoreUser newUser = new OrganicStoreUser
                {
                    UserName    = model.UserName,
                    Email       = model.Email,
                    PhoneNumber = model.PhoneNumber,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName
                };
                IdentityResult creationResult = await _signInManager.UserManager.CreateAsync(newUser);

                if (creationResult.Succeeded)
                {
                    //TODO: Create an account and log this user in
                    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); // This will format our token which might have the plus signs, dashes, etc

                        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);

                        #region use the SendGrid client to send a welcome email
                        var mailResult = await _emailService.SendEmailAsync(
                            model.Email,
                            "Welcome to Organic-Farm Store!",
                            "<p>Thanks for signing up, " + model.UserName + "!</p><p><a href=\"" + confirmationUrl + "\">Confirm your account<a></p>",
                            "Thanks for signing up, " + model.UserName + "!"
                            //"Thanks for signing up, " + model.UserName + "!",
                            //"<p>Thanks for signing up, " + model.UserName + "!</p>"
                            );

                        if (mailResult.Success)
                        {
                            return(RedirectToAction("RegisterConfirmation"));
                        }
                        else
                        {
                            return(BadRequest(mailResult.Message));
                        }
                        #endregion

                        //#region use the SendGrid client to send a welcome email
                        //var client = new SendGrid.SendGridClient(_sendGridKey);
                        //var senderAddress = new SendGrid.Helpers.Mail.EmailAddress("*****@*****.**", "CT O-Store");
                        //var subject = "Welcome to OrganicStore";
                        //var to = new SendGrid.Helpers.Mail.EmailAddress(model.Email, model.Email);
                        //var plainText = "Thanks for signing up, " + model.FirstName + "!";
                        //var htmlText = "<p> Thanks for signing up with us, " + model.FirstName + "!</p>";
                        //var message = SendGrid.Helpers.Mail.MailHelper.CreateSingleEmail(senderAddress, to, subject, plainText, htmlText);
                        //var mailResult = await client.SendEmailAsync(message);

                        //if ((mailResult.StatusCode == System.Net.HttpStatusCode.OK) || (mailResult.StatusCode == System.Net.HttpStatusCode.Accepted))
                        //    return RedirectToAction("RegisterConfirmation");
                        //else
                        //    return BadRequest(await mailResult.Body.ReadAsStringAsync());

                        //#endregion

                        //this._signInManager.SignInAsync(newUser, false);
                        // return RedirectToAction("SignIn", "Account");
                    }
                    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 RedirectToAction("Index", "Home");
            }
            return(View());
        }