Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SqlClient;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Configuration;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Data;
  10. namespace Lab5
  11. {
  12.     public partial class Login : System.Web.UI.Page
  13.     {
  14.         protected void Page_Load(object sender, EventArgs e)
  15.         {
  16.             if(!IsPostBack)
  17.             {
  18.                 HeaderHypLink.Visible = false;
  19.                 lblStatus.Text = "";
  20.                 lblStatus.Visible = false;
  21.                 lblCookie.Visible = false;
  22.                 lblCustomerid.Visible = false;
  23.                 lblFirstname.Visible = false;
  24.                 lblLastname.Visible = false;
  25.                 lblStatus.Visible = false;
  26.  
  27.                 HttpCookie cookie = Request.Cookies["Login"];
  28.                 if (cookie != null)
  29.                 {
  30.                     username.Text = Decode(cookie["UserName"]);
  31.                     //Display link to headers page
  32.                     HeaderHypLink.Visible = true;
  33.                     LogInButton.Visible = false;
  34.  
  35.                     Customer cust = new Customer(cookie["UserName"]);
  36.                     if (cust.Exists)
  37.                     {
  38.                         lblCookie.Text = "Cookie: " + cookie["UserName"];
  39.                         lblCustomerid.Text = "Customer ID: " + cust.CustomerID.ToString();
  40.                         lblFirstname.Text = "First Name: " + cust.FirstName;
  41.                         lblLastname.Text = "Last Name: " + cust.LastName;
  42.  
  43.                         lblCustomerid.Visible = true;
  44.                         lblFirstname.Visible = true;
  45.                         lblLastname.Visible = true;
  46.                         lblCookie.Visible = true;
  47.                     }
  48.                 }
  49.                 else
  50.                 {
  51.                     HeaderHypLink.Visible = false;
  52.                 }
  53.             }
  54.        
  55.  
  56.             // only run on first page load.
  57.             if (!IsPostBack)
  58.             {
  59.  
  60.             }
  61.             //Decoding/Encoding: http://stackoverflow.com/questions/11743160/how-do-i-encode-and-decode-a-base64-string
  62.             //Cookie info: https://msdn.microsoft.com/en-us/library/ms178194.aspx
  63.         }
  64.         //Login button code (when clicked)
  65.         protected void LogInButton_Click(object sender, EventArgs e)
  66.         {
  67.             lblCookie.Visible = false;
  68.             lblCustomerid.Visible = false;
  69.             lblFirstname.Visible = false;
  70.             lblLastname.Visible = false;
  71.             lblStatus.Visible = false;
  72.  
  73.             //Extract the username from the textbox at a maximum of 16 characters/letters
  74.             string usernameText = username.Text.Trim();
  75.             string passwordText = password.Text.Trim();
  76.  
  77.             //Check if the username or password textboxes were empty
  78.             if (usernameText == string.Empty || passwordText == string.Empty)
  79.             {
  80.                 lblStatus.Text = "username and password required";
  81.                 lblStatus.Visible = true;
  82.                 return;
  83.             }
  84.  
  85.             //Database stuff
  86.             string connectionString = WebConfigurationManager.ConnectionStrings["pubs"].ConnectionString;
  87.             SqlConnection con = new SqlConnection(connectionString);
  88.             try
  89.             {
  90.                 con.Open();
  91.  
  92.                 SqlCommand checkUserPass = new SqlCommand();
  93.                 checkUserPass.Connection = con;
  94.                 checkUserPass.CommandText = "SELECT customerid FROM customers WHERE username=@user AND password=@pass";
  95.                 checkUserPass.Parameters.AddWithValue("@user", usernameText);
  96.                 checkUserPass.Parameters.AddWithValue("@pass", passwordText);
  97.  
  98.                 SqlDataReader reader = checkUserPass.ExecuteReader();
  99.  
  100.                 // if an entry exists with the specified username and password
  101.                 if (reader.Read())
  102.                 {
  103.                     //Encode the username so that we can use it as a value for the cookie
  104.                     string encodedText;
  105.                     encodedText = generateEncoding(usernameText);
  106.  
  107.                     HttpCookie cookie = Request.Cookies["Login"];
  108.                     if (cookie == null)
  109.                     {
  110.                         cookie = new HttpCookie("Login");
  111.                     }
  112.  
  113.                     cookie["UserName"] = encodedText;
  114.                     cookie.Expires = DateTime.Now.AddDays(7);
  115.                     Response.Cookies.Add(cookie);
  116.  
  117.                     //Display link to headers page
  118.                     HeaderHypLink.Visible = true;
  119.  
  120.                     username.Text = Decode(cookie["UserName"]);
  121.  
  122.                     int customerID = (int)reader["customerid"];
  123.                     reader.Close();
  124.                     SqlCommand insertCookie = new SqlCommand();
  125.                     insertCookie.Connection = con;
  126.                     insertCookie.CommandText = "INSERT INTO sessions (customerid, cookie)"
  127.                         + " VALUES (@custID, @cookie)";
  128.                     insertCookie.Parameters.AddWithValue("@custID", customerID);
  129.                     insertCookie.Parameters.AddWithValue("@cookie", encodedText);
  130.                     insertCookie.ExecuteNonQuery();
  131.  
  132.                     Customer cust = new Customer(cookie["UserName"]);
  133.                     if (cust.Exists)
  134.                     {
  135.                         lblCookie.Text = "Cookie: " + cookie["UserName"];
  136.                         lblCustomerid.Text = "Customer ID: " + cust.CustomerID.ToString();
  137.                         lblFirstname.Text = "First Name: " + cust.FirstName;
  138.                         lblLastname.Text = "Last Name: " + cust.LastName;
  139.  
  140.                         lblCustomerid.Visible = true;
  141.                         lblFirstname.Visible = true;
  142.                         lblLastname.Visible = true;
  143.                         lblCookie.Visible = true;
  144.                     }
  145.  
  146.                     LogInButton.Visible = false;
  147.                 }
  148.                 else
  149.                 {
  150.                     lblStatus.Visible = true;
  151.                     lblStatus.Text = "Username or password doesn't exist.";
  152.                     return;
  153.                 }
  154.             }
  155.             catch (Exception ex)
  156.             {
  157.                 Response.Write(ex.Message);
  158.             }
  159.             finally
  160.             {
  161.                 con.Close();
  162.             }
  163.            
  164.         }
  165.  
  166.         //Takes in the username and spits it back out as a return value
  167.         private string generateEncoding(string text)
  168.         {
  169.             // Adds some noise characters to the beginning
  170.             text = generateNoiseCharacters() + text;
  171.             var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(text);
  172.             return Convert.ToBase64String(plainTextBytes);
  173.         }
  174.  
  175.         private string Decode(string encodedText)
  176.         {
  177.             try
  178.             {
  179.                 var base64EncodedBytes = Convert.FromBase64String(encodedText);
  180.                
  181.                 string decoded = System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
  182.                 decoded = decoded.Substring(3);
  183.                 return decoded;
  184.             }
  185.             catch(Exception)
  186.             {
  187.                 Response.Write("Your user setting were weird: " + encodedText);
  188.                 return "";
  189.             }
  190.         }
  191.  
  192.         private static string generateNoiseCharacters(int length = 3)
  193.         {
  194.             Random random = new Random();
  195.  
  196.             const string chars = "ABCDEFGHIJKLMNOPQRSTUBWXYZ12345678I90!#$%^&*()-=_+/.,;'?><";
  197.             // select length amount of random characters from the chars string, and return those.
  198.             return new string(Enumerable.Repeat(chars, length).Select(s => s[random.Next(s.Length)]).ToArray());
  199.         }
  200.     }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement