public CustomerLoginResponse ValidateCustomerLogin(CustomerLoginInfo customerLoginInfo)
        {
            CustomerLoginResponse customerLoginResponse = new CustomerLoginResponse();
            string customerID           = string.Empty;
            string password             = string.Empty;
            bool   isCustomerRegistered = false;

            try
            {
                using (SqlCommand command = new SqlCommand())
                {
                    command.Connection  = _connection;
                    command.CommandType = CommandType.Text;
                    command.CommandText = @"SELECT * FROM CustomerLoginDetails WHERE CustomerID=@Id";
                    command.Parameters.AddWithValue("@Id", customerLoginInfo.CustomerID);

                    if (_connection.State == ConnectionState.Closed)
                    {
                        _connection.Open();
                    }
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                password   = reader["Password"].ToString();
                                customerID = reader["CustomerID"].ToString();
                            }
                            isCustomerRegistered = true;
                        }
                    }

                    if (isCustomerRegistered && customerLoginInfo.Password.Equals(password) && customerLoginInfo.CustomerID.Equals(customerID))
                    {
                        customerLoginResponse.CustomerLoginStatus = true;
                        //creating JWT token

                        var secretKey         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("superSecretKey@345"));
                        var signinCredentials = new SigningCredentials(secretKey, SecurityAlgorithms.HmacSha256);

                        var tokeOptions = new JwtSecurityToken(
                            issuer: "http://localhost:49366",
                            audience: "http://localhost:49366",
                            claims: new List <Claim>(),
                            expires: DateTime.Now.AddMinutes(1),
                            signingCredentials: signinCredentials
                            );

                        var tokenString = new JwtSecurityTokenHandler().WriteToken(tokeOptions);

                        customerLoginResponse.jsonToken = tokenString;


                        if (AreDetailsSaved(customerLoginInfo.CustomerID, UserType.Customer))
                        {
                            customerLoginResponse.AreCustomerAdditionalDetailsSaved = true;
                        }

                        else
                        {
                            customerLoginResponse.AreCustomerAdditionalDetailsSaved = false;
                        }
                    }
                    else
                    {
                        customerLoginResponse.CustomerLoginStatus = false;
                        throw new Exception("Incorrect Passowrd.Please enter valid credentials/your are not registered");
                    }
                }
            }
            catch (Exception ex)
            {
                customerLoginResponse.ErrorMessage = ex.Message;
            }
            finally
            {
                _connection.Close();
            }

            return(customerLoginResponse);
        }
        private void CreateCustomer(CustomerModel customer)
        {
            // Create or Update the customerAccount
            var customerAccountResource = new CustomerAccountResource(Context.ApiContext);

            var existingAccount = ExistingAccount(customer.Account.UserName);

            if (existingAccount != null)
            {
                // Update existing account
                customer.Account.Id = existingAccount.Id;

                customer.Account = customerAccountResource.UpdateAccount(customer.Account, customer.Account.Id);
                ReportProgress("Account updated: " + customer.Account.Id);
            }
            else
            {
                // Add a new account
                customer.Account = customerAccountResource.AddAccount(customer.Account);
                ReportProgress("Account created: " + customer.Account.Id + " " + customer.Account.UserName);
            }

            // Set the password only if we have one
            if (!string.IsNullOrEmpty(customer.Password))
            {
                var loginInfo = new CustomerLoginInfo();
                loginInfo.EmailAddress = customer.Account.EmailAddress;
                loginInfo.IsImport     = true;
                loginInfo.Username     = customer.Account.UserName;
                loginInfo.Password     = customer.Password;
                var customerAuth = customerAccountResource.AddLoginToExistingCustomer(loginInfo, customer.Account.Id);
                ReportProgress("Password Updated for : " + customer.Account.Id);
            }

            foreach (var contact in customer.Contacts)
            {
                // Update or Create the customer contact as required
                var customerContactResource = new CustomerContactResource(Context.ApiContext);

                // Find the existing contact of this type.
                CustomerContact existingContact = null;
                if (customer.Account.Contacts != null)
                {
                    foreach (var cc in customer.Account.Contacts)
                    {
                        foreach (var t in cc.Types)
                        {
                            if (t.Name == contact.Types[0].Name)
                            {
                                existingContact = cc;
                                break; // out
                            }
                            if (existingContact != null)
                            {
                                break; // out
                            }
                        }
                    }
                }

                if (existingContact != null)
                {
                    // update the existing contact
                    contact.Id = existingContact.Id;
                    customerContactResource.UpdateAccountContact(contact, customer.Account.Id, existingContact.Id);
                    ReportProgress("contact Updated: " + contact.Id + " " + contact.Email);
                }
                else
                {
                    // create a new contact
                    var newContact = customerContactResource.AddAccountContact(contact, customer.Account.Id);
                    ReportProgress("Contact Created Id: " + newContact.Id + " for " + newContact.Email);
                }
            }
        }