• (3 Pages)
  • +
  • 1
  • 2
  • 3

C# Login/Register system using SQL Compact

#31 addanny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-March 14

Posted 21 March 2014 - 06:52 PM

After finishing the WHOLE code and running it (there are no errors) the application does not function whatsoever. I enter my username and password and then configure but when I register the whole thing just freezes. ANY thoughts now?
this is what i have for code:
using System;
using System.Net;
using System.Net.Mail;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace LoginSystem
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void loginsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.loginsBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.loginsDataSet);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'loginsDataSet.Logins' table. You can move, or remove it, as needed.
            this.loginsTableAdapter.Fill(this.loginsDataSet.Logins);

        }

        private void registerButton_Click(object sender, EventArgs e)
        {
            AddUser(registerUserName.Text, registerPassword.Text, registerConfirmPassword.Text, registerEmail.Text);
        }

        string email;
        private void AddUser(string username, string password, string confirmPass, string email)
        {

            //Local variables to hold values
            string smtpEmail = smtpUserNameTextBox.Text;
            string smtpPassword = smtpPasswordTextBox.Text;
            int smtpPort = (int)smtpPortNumericUD.Value;
            string smtpAddress = smtpAddressTextBox.Text;

            //Regex for making sure Email is valid
            Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
            Match match = regex.Match(email);

            //Loop through Logins table
            foreach (DataRow row in loginsDataSet.Logins)
            {

                //And look for matching usernames
                if (row.ItemArray[0].Equals(username))
                {
                    //If one is found, show message:
                    MessageBox.Show("Username already exists");
                    return;
                }
            }

            //Confirm pass must equal password.
            if (password != confirmPass)
            {
                MessageBox.Show("Passwords do not match");
            }
            //Password must be at least 6 characters long
            else if (password.Length < 6)
            {
                MessageBox.Show("Password must be at least 6 characters long");
            }
            //If email is NOT valid
            else if (!match.Success)
            {
                MessageBox.Show("Invalid Email");
            }
            //If there is no usrename
            else if (username == null)
            {
                MessageBox.Show("Must have Username");
            }
            //If all is well, create the new user!
            else
            {
                loginsDataSet.LoginsRow newUserRow = loginsDataSet.Logins.NewLoginsRow();

                string EncryptedPass = HashPass(password);

                newUserRow.Username = username;
                newUserRow.Password = EncryptedPass;
                newUserRow.Email = email;

                loginsDataSet.Logins.Rows.Add(newUserRow);
                registerUserName.Text = String.Empty;
                registerPassword.Text = String.Empty;
                registerConfirmPassword.Text = String.Empty;
                registerEmail.Text = String.Empty;

                MessageBox.Show("Thank you for registering!");

                if (String.IsNullOrWhiteSpace(smtpEmail) || String.IsNullOrWhiteSpace(smtpPassword) || String.IsNullOrWhiteSpace(smtpAddress) || smtpPort <= 0)
                {
                    MessageBox.Show("Email Configuration is not set up Correctly! \nCannot send email!");
                }
                else
                {
                    SendMessage(email.ToString(), username.ToString(), password.ToString());
                }
            }

        }
        public void SendMessage(string ToAddress, string ToName, string password)
        {
            var client = new SmtpClient(smtpAddressTextBox.Text, (int)smtpPortNumericUD.Value)
            {
                Credentials = new NetworkCredential(smtpUserNameTextBox.Text, smtpPasswordTextBox.Text),
                EnableSsl = true
            };

            client.Send(smtpUserNameTextBox.Text, ToAddress,"Thank You","Thank you for registering with us today! \n Your username/passwords are: \n \nUsername: " + ToName.ToString() + "\nPassword: " + password.ToString()); 
        }
        public string HashPass(string password)                        
        {
            
            SHA256 sha = new SHA256CryptoServiceProvider();
            
            //compute hash from the bytes of text
            sha.ComputeHash(ASCIIEncoding.ASCII.GetBytes(password + email));

            //gethash result after compute it
            byte[] result = sha.Hash;

            StringBuilder strBuilder = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                //change it into 2 hexadecimal digits
                //for each byte
                strBuilder.Append(result[i].ToString("x2"));
            }
            return strBuilder.ToString();
        }

        private void loginButton_Click(object sender, EventArgs e)
        {
            //Locals to hold values
            string username = loginUserName.Text;
            string password = HashPass(loginPassword.Text);

            //Loop through database
            foreach (DataRow row in loginsDataSet.Logins)
            {
                //And search for Username and Pass that match
                if (row.ItemArray[0].Equals(username) && row.ItemArray[1].Equals(password))
                {
                    loginUserName.Text = String.Empty;
                    loginPassword.Text = String.Empty;
                    MessageBox.Show("Login Success");
                    break;
                }
                //If not, then show this message.
                else
                {
                    MessageBox.Show("Username/Password incorrect");
                    break;
                }
            }
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#32 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9570
  • View blog
  • Posts: 36,242
  • Joined: 12-June 08

Posted 22 March 2014 - 07:07 AM

Freezes? I would suggest start putting in breakpoints along the execution flow to determine where the problem is.
Was This Post Helpful? 0
  • +
  • -

#33 crj3565  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-March 14

Posted 29 March 2014 - 11:07 PM

Im receiving an email error message [The name 'email'does not exist in current context.
Error Message#2.....When I loging the loop will only check the first user in the table. It will not give me a successful login on any names after the first.

public string HashPass(string password)
        {
            SHA256 sha = new SHA256CryptoServiceProvider();

            //compute hash from the bytes of text
            sha.ComputeHash(ASCIIEncoding.ASCII.GetBytes(password + email));

            //get hash result after compute it
            byte[] result = sha.Hash;

            StringBuilder strBuilder = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                //change it into 2 hexadecimal digits
                //for each byte
                strBuilder.Append(result[i].ToString("x2"));
            }

            return strBuilder.ToString();
        }


This post has been edited by macosxnerd101: 29 March 2014 - 11:25 PM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#34 crj3565  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-March 14

Posted 30 March 2014 - 12:06 AM

I'm having trouble getting the loop through database to work correctly, the first username and password will match, but after that on it will not continue through the database. Giving me a user/password error
private void loginButton_Click(object sender, EventArgs e)
{
    //Locals to hold values
    string username = loginUserName.Text;
    string password = HashPass(loginPassword.Text);


    //Loop through database
    foreach (DataRow row in loginsDataSet.Logins)
    {
        //And search for Username and Pass that match
        if (row.ItemArray[0].Equals(username) && row.ItemArray[1].Equals(password))
        {
            loginUserName.Text = String.Empty;
            loginPassword.Text = String.Empty;
            MessageBox.Show("Login Success");
            break;
        }
        //If not, then show this message.
        else
        {
            MessageBox.Show("Username/Password incorrect");
            break;
        }
    }
} 








Does anyone tested this ? I did it and it searches only the 1st Row for valid user/pass and then doesnt bother to search the rest of the table , it just gets into else shows the messabox and breaks, so basically if you dont have a valid user/pass in the fisrt row that code becomes useless? Do i missed something there?



I did this instead to make it work properly, maybe try/catch can work too?

 

            Boolean logfail = true;

            foreach (DataRow row in eofficeDataSet.admin)
            {
                if (row.ItemArray[1].Equals(username) && row.ItemArray[2].Equals(password))
                {
                    textBox1.Text = String.Empty;
                    textBox2.Text = String.Empty;
                    MessageBox.Show("Login Success");
                    logfail = false;
                    fmenu f2 = new fmenu();
                    f2.Show();    
                    break;
                }
            }

                   if (logfail==true)
                {

                     MessageBox.Show("Username/Password incorrect");
                }
                








Also when i was doing register the data was not saved in the database , adding this line

this.tableAdapterManager.UpdateAll(this.eofficeDataSet);

to get my new entry saved.


Do i missed something when i did the original code because i m really new to this stuff ...

This post has been edited by macosxnerd101: 30 March 2014 - 12:07 AM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3