예제 #1
0
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            //connect
            using (DefaultConnection db = new DefaultConnection())
            {
                //create instructor object
                Staff objS = new Staff();

                //first get salt value for this username
                String username = txtUsername.Text;

                objS = (from s in db.Staff
                        where s.username == username
                        select s).FirstOrDefault();

                //did we find this username?
                if (objS != null)
                {
                    String salt = objS.salt;

                    //salt and hash the plain text password
                    String password = txtPassword.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 we just salted and hashed matches the password in the db
                    if (objS.hashed == base64)
                    {
                        //lblError.Text = "Valid Login";
                        //store the identity in the session object
                        Session["AdminID"] = objS.AdminID;

                        //redirect to departments page
                        Response.Redirect("welcome.aspx");
                    }
                    else
                    {
                        lblError.Text = "Invalid Login";
                    }
                }
                else
                {
                    lblError.Text = "Invalid Login";
                }
            }
        }
예제 #2
0
        protected void btnRegister_Click(object sender, EventArgs e)
        {
            //connect
            using (DefaultConnection db = new DefaultConnection())
            {
                //create a new instructor
                Staff objI = new Staff();

                //fill the properties from the form inputs
                objI.username = txtUsername.Text;
                // will store objI.hashed later once password is encrypted

                //salt and hash the plain 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.hashed = base64;
                objI.salt = salt;

                //save
                db.Staff.Add(objI);
                db.SaveChanges();

                //redirect
                Response.Redirect("login.aspx");
            }
        }