//=========================================================================================== //Login //=========================================================================================== protected void btnAdminLogin_Click(object sender, EventArgs e) { //Connecting to the Database. using (DefaultConnection1 db = new DefaultConnection1()) { //Creating user in memory. admin_users objI = new admin_users(); //First get the salt value for this username String username = txtUsernameLogin.Text; objI = (from em in db.admin_users where em.admin_username == username select em).FirstOrDefault(); //Did the username match? if (objI != null) { //Grabing salt. String salt = objI.admin_salt; //Salt and hash the plan text password. String password = txtPasswordLogin.Text; String pass_and_salt = password + salt; // Create a new instance of the hash crypto service provider. HashAlgorithm hashAlg = new SHA256CryptoServiceProvider(); // Convert the data to hash to an array of Bytes. byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(pass_and_salt); // Compute the Hash. This returns an array of Bytes. byte[] bytHash = hashAlg.ComputeHash(bytValue); // Optionally, represent the hash value as a base64-encoded string, // For example, if you need to display the value or transmit it over a network. string base64 = Convert.ToBase64String(bytHash); //Check if the password that was just salted and hashed matches the password in the database. if (objI.admin_password == base64) { //Checking if the password was the same, Showing a valid login. lblSubmitLoginMessage.Text = "Valid Login"; //Store the indentity in the session object. Session["admin_user_id"] = objI.admin_user_id; //Rediect to logged in homepage. Response.Redirect("admin_grid.aspx"); } else { lblSubmitLoginMessageError.Text = "Invaild Login, the Email or Password may be wrong."; } } } }
protected void GetUser() { //Connecting to Database. using (DefaultConnection1 db = new DefaultConnection1()) { var ur = from u in db.public_users select u; //bind the us query results to the grid grdPublicUsers.DataSource = ur.ToList(); grdPublicUsers.DataBind(); } }
/** * <summary> * This method gets the student data from the DB * </summary> * * @method GetStudents * @returns {void} */ protected void GetStudents() { // connect to EF using (DefaultConnection1 db = new DefaultConnection1()) { // query the Students Table using EF and LINQ var Students = (from allStudents in db.Students select allStudents); // bind the result to the GridView StudentsGridView.DataSource = Students.ToList(); StudentsGridView.DataBind(); } }
protected void GetExercise() { //using entity framework to connect and get the list of food. using (DefaultConnection1 db = new DefaultConnection1()) { Int32 user_id = Convert.ToInt32(Session["user_id"]); var ex = from x in db.exercises where x.user_id == user_id select x; // bind the ex query result to the grid grdExercise.DataSource = ex.ToList(); grdExercise.DataBind(); } }
protected void GetExercise() { //look up the selected exercise and fill the exercise edit form. using (DefaultConnection1 db = new DefaultConnection1()) { //Store the ID from the URL in the variable. Int32 exercise_id = Convert.ToInt32(Request.QueryString["exercise_id"]); //Look up the food exercise ex = (from x in db.exercises where x.exercise_id == exercise_id select x).FirstOrDefault(); //pre populate the form feilds txtExerciseName.Text = ex.exercise_name; txtReps.Text = ex.exercise_rep; txtExerciseDate.Text = Convert.ToString(ex.exercise_date); } }
protected void btnAddFood_Click(object sender, EventArgs e) { //connect using (DefaultConnection1 db = new DefaultConnection1()) { //create a new food in memory food fd = new food(); Int32 food_id = 0; //check for a url if (!String.IsNullOrEmpty(Request.QueryString["food_id"])) { //get the id from the url food_id = Convert.ToInt32(Request.QueryString["food_id"]); //look up the food fd = (from f in db.foods where f.food_id == food_id select f).FirstOrDefault(); } //importing the user_id from the session object. Int32 user_id = Convert.ToInt32(Session["user_id"]); //fill the properties of the new food entry fd.food_name = txtFoodName.Text; fd.food_calories = Convert.ToInt32(txtCalories.Text); fd.food_date = Convert.ToDateTime(txtDate.Text); fd.user_id = user_id; if (food_id == 0) { db.foods.Add(fd); } //Save the food to the database. db.SaveChanges(); //redierect to the food list page. Response.Redirect("food_tracker.aspx"); } }
protected void grdPublicUsers_RowDeleting(object sender, GridViewDeleteEventArgs e) { Int32 user_id = Convert.ToInt32(grdPublicUsers.DataKeys[e.RowIndex].Values["user_id"]); //Connecting to Database. using (DefaultConnection1 db = new DefaultConnection1()) { public_users ur = (from u in db.public_users where u.user_id == user_id select u).FirstOrDefault(); //Deleting the user. db.public_users.Remove(ur); db.SaveChanges(); //Refreshing the grid. GetUser(); } }
protected void grdExercise_RowDeleting(object sender, GridViewDeleteEventArgs e) { //Identify the excercise ID to be deleted from the row that the user selected from Int32 exercise_id = Convert.ToInt32(grdExercise.DataKeys[e.RowIndex].Values["exercise_id"]); //Connect using (DefaultConnection1 db = new DefaultConnection1()) { exercise ex = (from x in db.exercises where x.exercise_id == exercise_id select x).FirstOrDefault(); //delete db.exercises.Remove(ex); db.SaveChanges(); //refresh GetExercise(); } }
protected void GetFood() { //look up the seleted food and fill the edit form. using (DefaultConnection1 db = new DefaultConnection1()) { //store the id from the URL in a variable. Int32 food_id = Convert.ToInt32(Request.QueryString["food_id"]); //look up the food food fd = (from f in db.foods where f.food_id == food_id select f).FirstOrDefault(); // pre populate the form fields txtFoodName.Text = fd.food_name; txtCalories.Text = Convert.ToString(fd.food_calories); txtDate.Text = Convert.ToString(fd.food_date); } }
protected void grdFood_RowDeleting(object sender, GridViewDeleteEventArgs e) { //indentify the food ID to be deleted from the row the user seleted from. Int32 food_id = Convert.ToInt32(grdFood.DataKeys[e.RowIndex].Values["food_id"]); //connect using (DefaultConnection1 db = new DefaultConnection1()) { food fd = (from f in db.foods where f.food_id == food_id select f).FirstOrDefault(); //delete db.foods.Remove(fd); db.SaveChanges(); //refresh GetFood(); } }
protected void GetFood() { //using entity framework to connect and get the list of food. using (DefaultConnection1 db = new DefaultConnection1()) { //old query that shows all the food in the database. //var fd = from f in db.foods // select f; //new query filtered for logged in user only. Int32 user_id = Convert.ToInt32(Session["user_id"]); var fd = from f in db.foods where f.user_id == user_id select f; //bind the fd query result to the grid grdFood.DataSource = fd.ToList(); grdFood.DataBind(); } }
protected void btnAddExercise_Click(object sender, EventArgs e) { //Connect using (DefaultConnection1 db = new DefaultConnection1()) { //create new exercise in memory exercise ex = new exercise(); Int32 exercise_id = 0; //check for URL if (!String.IsNullOrEmpty(Request.QueryString["exercise_id"])) { //get the id from the url exercise_id = Convert.ToInt32(Request.QueryString["exercise_id"]); //Look up the food ex = (from x in db.exercises where x.exercise_id == exercise_id select x).FirstOrDefault(); } //importing the user_id from the session object. Int32 user_id = Convert.ToInt32(Session["user_id"]); //fill the properties of the new Exercise ex.exercise_name = txtExerciseName.Text; ex.exercise_rep = txtReps.Text; ex.exercise_date = Convert.ToDateTime(txtExerciseDate.Text); ex.user_id = user_id; if (exercise_id == 0) { db.exercises.Add(ex); } //Save the exercise to the data base db.SaveChanges(); //redirect to the exercise tracker page Response.Redirect("exercise_tracker.aspx"); } }
protected void btnSignUp_Click(object sender, EventArgs e) { //connect using (DefaultConnection1 db = new DefaultConnection1()) { //create a new user user objI = new user(); //fill the properties from the form inputs objI.first_name = txtFName.Text; objI.last_name = txtLName.Text; objI.email = txtEmail.Text; //salt and hash the plan text password. String password = txtPassword.Text; String salt = CreateSalt(8); String pass_and_salt = password + salt; // Create a new instance of the hash crypto service provider. HashAlgorithm hashAlg = new SHA256CryptoServiceProvider(); // Convert the data to hash to an array of Bytes. byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(pass_and_salt); // Compute the Hash. This returns an array of Bytes. byte[] bytHash = hashAlg.ComputeHash(bytValue); // Optionally, represent the hash value as a base64-encoded string, // For example, if you need to display the value or transmit it over a network. string base64 = Convert.ToBase64String(bytHash); objI.password = base64; objI.salt = salt; //save db.users.Add(objI); db.SaveChanges(); } }
//=========================================================================================== //Signup //=========================================================================================== protected void btnAdminSignUp_Click(object sender, EventArgs e) { //Connect to the Database. using (DefaultConnection1 db = new DefaultConnection1()) { //Creating a new user. admin_users objI = new admin_users(); //Fill the properties from the form inputs. objI.admin_username = txtUsernameSignUp.Text; //Salt and Hash the plan text Password. String password = txtPasswordSignUp.Text; String salt = CreateSalt(8); String pass_and_salt = password + salt; // Create a new instance of the hash crypto service provider. HashAlgorithm hashAlg = new SHA256CryptoServiceProvider(); // Convert the data to hash to an array of Bytes. byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(pass_and_salt); // Compute the Hash. This returns an array of Bytes. byte[] bytHash = hashAlg.ComputeHash(bytValue); // Optionally, represent the hash value as a base64-encoded string, // For example, if you need to display the value or transmit it over a network. string base64 = Convert.ToBase64String(bytHash); //Filling the properties of password to Database. objI.admin_password = base64; objI.admin_salt = salt; //Saving information into Database. db.admin_users.Add(objI); db.SaveChanges(); } }
protected void SaveButton_Click(object sender, EventArgs e) { // Use EF to connect to the server using (DefaultConnection1 db = new DefaultConnection1()) { // use the Student model to create a new student object and // save a new record Student newStudent = new Student(); // add form data to the new student record newStudent.LastName = LastNameTextBox.Text; newStudent.FirstMidName = FirstNameTextBox.Text; newStudent.EnrollmentDate = Convert.ToDateTime(EnrollmentDateTextBox.Text); // use LINQ to ADO.NET to add / insert new student into the database db.Students.Add(newStudent); // save our changes db.SaveChanges(); // Redirect back to the updated students page Response.Redirect("~/Students.aspx"); } }
//=========================================================================================== //Signup //=========================================================================================== protected void btnDealerSignUp_Click(object sender, EventArgs e) { //Connecting to Database. using (DefaultConnection1 db = new DefaultConnection1()) { //Creating a new user. public_users objI = new public_users(); //fill the properties from the form inputs. objI.contact_name = txtContactNameSignUp.Text; objI.email = txtContactEmailSignUp.Text; objI.company_name = txtCompanyNameSignUp.Text; objI.phone = txtContactPhoneNumberSignUp.Text; objI.fax = txtContactFaxSignUp.Text; objI.address_line_1 = txtAddressSignUp.Text; objI.address_line_2 = txtAddressLine2SignUp.Text; objI.city = txtCitySignUp.Text; objI.state_province = txtStateProvinceSignUp.Text; objI.zip_postal = txtZipPostalSignUp.Text; //Salt and Hash the plan text Password. String password = txtPasswordSignUp.Text; String salt = CreateSalt(8); String pass_and_salt = password + salt; // Create a new instance of the hash crypto service provider. HashAlgorithm hashAlg = new SHA256CryptoServiceProvider(); // Convert the data to hash to an array of Bytes. byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(pass_and_salt); // Compute the Hash. This returns an array of Bytes. byte[] bytHash = hashAlg.ComputeHash(bytValue); // Optionally, represent the hash value as a base64-encoded string, // For example, if you need to display the value or transmit it over a network. string base64 = Convert.ToBase64String(bytHash); //Filling the properties of password to Database. objI.password = base64; objI.salt = salt; //Saving information into Database. db.public_users.Add(objI); db.SaveChanges(); } //Email form information to [email protected] //Getting Informaion from form inputs. String Name = txtContactNameSignUp.Text; String CompanyName = txtCompanyNameSignUp.Text; String Email = txtContactEmailSignUp.Text; String PhoneNumber = txtContactPhoneNumberSignUp.Text; String Fax = txtContactFaxSignUp.Text; String AddressLine1 = txtAddressSignUp.Text; String AddressLine2 = txtAddressLine2SignUp.Text; String City = txtCitySignUp.Text; String StateProvince = txtStateProvinceSignUp.Text; String ZipPostal = txtZipPostalSignUp.Text; String Password = txtPasswordSignUp.Text; try { MailMessage mail = new MailMessage(); mail.To.Add("*****@*****.**"); //Change to [email protected] when launching. mail.From = new MailAddress("*****@*****.**"); mail.Subject = "New Dealer Signed Up: " + Name + " from " + CompanyName; string Body = "<h1>New Dealer Signed Up</h1>" + "<br />" + "<b>Name:</b>" + " " + Name + "<br />" + "<b>Comapny Name:</b>" + " " + CompanyName + "<br />" + "<b>Contact Email:</b>" + " " + Email + "<br />" + "<b>Contact Phone Number:</b>" + " " + PhoneNumber + "<br />" + "<b>Contact Fax:</b>" + " " + Fax + "<br />" + "<h2>Address</h2>" + "<b>Address Line 1:</b>" + "<br />" + AddressLine1 + "<br />" + "<b>Address Line 2:</b>" + "<br />" + AddressLine2 + "<br />" + "<b>City:</b>" + "<br />" + City + "<br />" + "<b>State / Province:</b>" + "<br />" + StateProvince + "<br />" + "<b>Zip Code / Postal Code:</b>" + "<br />" + AddressLine1 + "<br />" + "<h2>Password</h2>" + "<b>Password:</b>" + "<br />" + Password + "<br />" + "<h2>Manage Users</h2>" + "<a href='http://signservice.com/admin/admin_login.aspx'>Manage Users</a>"; mail.Body = Body; mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = "mail.signservicelabelproducts.com"; //Or Your SMTP Server Address smtp.Credentials = new System.Net.NetworkCredential ("*****@*****.**", "3Mavsol3*"); smtp.Port = 1025; //Or your Smtp Email ID and Password smtp.EnableSsl = false; smtp.Send(mail); lblSubmitSignUpMessage.Text = "You can sign in now."; } catch { lblSubmitSignUpMessageError.Text = "Something went wrong! Please try again."; } }
protected void btnLogin_Click(object sender, EventArgs e) { //connect using (DefaultConnection1 db = new DefaultConnection1()) { //create user object in memory user objI = new user(); //first get the salt value for this username String email = txtEmailLogin.Text; objI = (from em in db.users where em.email == email select em).FirstOrDefault(); //did the email find a match? if (objI != null) { String salt = objI.salt; //salt and hash the plan text password. String password = txtPasswordLogin.Text; String pass_and_salt = password + salt; // Create a new instance of the hash crypto service provider. HashAlgorithm hashAlg = new SHA256CryptoServiceProvider(); // Convert the data to hash to an array of Bytes. byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(pass_and_salt); // Compute the Hash. This returns an array of Bytes. byte[] bytHash = hashAlg.ComputeHash(bytValue); // Optionally, represent the hash value as a base64-encoded string, // For example, if you need to display the value or transmit it over a network. string base64 = Convert.ToBase64String(bytHash); //check if the password that was just salted and hashed matches the password in the database. if (objI.password == base64) { //Checking if the password was the same, Showing a valid login. //lblError.Text = "Valid Login"; //store the identity in the session object Session["user_id"] = objI.user_id; Session["user_name"] = objI.first_name = " " + objI.last_name; //rediect to logged in homepage. Response.Redirect("login_landing.aspx"); } else { lblError.Text = "Invaild Login"; } } else { lblError.Text = "Invalid Login"; } } }