Guest User

changed

a guest
Jul 27th, 2017
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using System.Security.Cryptography;
  10. using System.ComponentModel;
  11. using System.Text;
  12.  
  13. public partial class Login : System.Web.UI.Page
  14. {
  15.     protected void Page_Load(object sender, EventArgs e)
  16.     {
  17.         loginFail.Visible = false;
  18.         emailFail.Visible = false;
  19.     }
  20.  
  21.     protected void loginButton_Click(object sender, System.EventArgs e)
  22.     {
  23.         string IndvConnString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLAzureConnection"].ConnectionString;
  24.         SqlConnection IndvSignCon = new SqlConnection(IndvConnString);
  25.  
  26.         IndvSignCon.Open();
  27.  
  28.         SqlCommand cmd;
  29.         SqlDataReader rdr;
  30.  
  31.         Type checking = this.GetType();
  32.         string checklogin = "SELECT * FROM Customer";
  33.         cmd = new SqlCommand(checklogin, IndvSignCon);
  34.         rdr = cmd.ExecuteReader();
  35.  
  36.  
  37.         while (rdr.Read() == true)
  38.         {
  39.             if (email_textbox.Text == "" || password_textbox.Text == "")
  40.             {
  41.                 return;
  42.             }
  43.             else
  44.             {
  45.  
  46.                 if (email_textbox.Text == (string)rdr["cEmail"])
  47.                 {
  48.                     string usernameSTR = email_textbox.Text;
  49.  
  50.                     string getUserIDStr = System.Configuration.ConfigurationManager.ConnectionStrings["SQLAzureConnection"].ConnectionString;
  51.  
  52.                     SqlConnection getUserIDCon = new SqlConnection(getUserIDStr);
  53.  
  54.                     SqlCommand getUserIdCMD;
  55.  
  56.                     string userIdSTR = "SELECT cID FROM Customer WHERE cEmail = @UserName";
  57.  
  58.                     getUserIdCMD = new SqlCommand(userIdSTR, getUserIDCon);
  59.  
  60.                     getUserIdCMD.Parameters.AddWithValue("@UserName", SqlDbType.VarChar).Value = usernameSTR;
  61.  
  62.                     getUserIDCon.Open();
  63.  
  64.                     SqlDataReader getUserIdRDR = getUserIdCMD.ExecuteReader();
  65.  
  66.                     while (getUserIdRDR.Read() == true)
  67.                     {
  68.                         int userIDInt = (int)getUserIdRDR["cID"];
  69.  
  70.                         string MatchHashStr = System.Configuration.ConfigurationManager.ConnectionStrings["SQLAzureConnection"].ConnectionString;
  71.  
  72.                         SqlConnection MatchHashCON = new SqlConnection(MatchHashStr);
  73.  
  74.                         SqlCommand MatchHashCMD;
  75.  
  76.                         //Compare HashValue
  77.  
  78.                         string CompareHashStr = null;
  79.  
  80.                         byte[] compareSource;
  81.  
  82.                         byte[] compareHash;
  83.  
  84.                         CompareHashStr = password_textbox.Text;
  85.  
  86.                         compareSource = ASCIIEncoding.ASCII.GetBytes(CompareHashStr);
  87.  
  88.                         compareHash = new MD5CryptoServiceProvider().ComputeHash(compareSource);
  89.  
  90.  
  91.  
  92.                         string MatchHashSQLStr = "SELECT cEmail FROM Customer WHERE cID = @CustomerID AND cPassword = @CompareHash";
  93.  
  94.                         MatchHashCMD = new SqlCommand(MatchHashSQLStr, MatchHashCON);
  95.  
  96.                         MatchHashCMD.Parameters.AddWithValue("@CustomerID", SqlDbType.Int).Value = userIDInt;
  97.                         MatchHashCMD.Parameters.Add("@CompareHash", SqlDbType.VarBinary, 16).Value = compareHash;
  98.  
  99.                         MatchHashCON.Open();
  100.  
  101.                         SqlDataReader MatchHashRDR = MatchHashCMD.ExecuteReader();
  102.  
  103.                         while (MatchHashRDR.Read() == true)
  104.                         {
  105.                             string exstUserNameStr = (string)rdr["cEmail"].ToString() ;
  106.  
  107.                             if (exstUserNameStr == usernameSTR)
  108.                             {
  109.                                 Session["sFlag"] = "T"; // sFlag = "T" means user has logged in
  110.                                 Session["sName"] = rdr["cFullName"];
  111.                                 Session["sAddress"] = rdr["cAddress"];
  112.                                 Session["sEmail"] = rdr["cEmail"];
  113.                                 Session["sMobile"] = rdr["cHp"];
  114.                                 Session["sCompany"] = rdr["cCompanyName"];
  115.                                 Session["sCustomerID"] = rdr["cID"].ToString(); //ToString() Convert Integer to String              
  116.                                 string strCustomerId = (string)Session["sCustomerID"];
  117.                                 DataRoutines DRObject = new DataRoutines();
  118.                                 DRObject.CreateRecs(strCustomerId);
  119.  
  120.  
  121.                                 IndvSignCon.Close();
  122.                                 getUserIDCon.Close();
  123.                                 MatchHashCON.Close();
  124.                                 Response.Redirect("MyAccount.aspx");
  125.                             }
  126.                             else
  127.                             {
  128.                                 Session["sFlag"] = "F";
  129.                                 Session["sName"] = "";
  130.                                 Session["sCustomerID"] = "";
  131.  
  132.                                 loginFail.Visible = true;
  133.                             }
  134.                         }
  135.                         MatchHashCON.Close();
  136.  
  137.                     }
  138.  
  139.                     getUserIDCon.Close();
  140.  
  141.                 }
  142.                 else
  143.                 {
  144.  
  145.                     emailFail.Visible = true;
  146.                 }
  147.             }
  148.         }
  149.         IndvSignCon.Close();
  150.     }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment