10 Replies - 8286 Views - Last Post: 30 May 2010 - 03:55 AM Rate Topic: -----

#1 rabmat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-May 10

how to remember password using checkbox ?

Posted 29 May 2010 - 04:25 PM

Hello fellows
i am new in the field, i am designing a login form in windows application with username password and checkbox
i am using c# MS SQL server
i managed to write code to verify user name and password by comparing them to data stored in database
but i am stock in how to save the username password if the user checks the remember password checkbox
ofcurs remember password for next login if the login successed
and i need code to forget login data if checkbox is not checked
i created a table named Login with 3 columns UName Password chk
the chk column is to store the checked status of the checkbox but i don't have any idea what type it should be in sql server and what to store in it.
here is the project http://uploading.com...8de/Clinic.rar/
so you can review it and modify it as neccecary.

This post has been edited by rabmat: 29 May 2010 - 04:34 PM


Is This A Good Question/Topic? 0
  • +

Replies To: how to remember password using checkbox ?

#2 elbielefeld  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 217
  • Joined: 18-May 10

Re: how to remember password using checkbox ?

Posted 29 May 2010 - 05:25 PM

Hi rabmat,

you can't store a value in the db indicating if the user wants the program to remember his user/pass. As the program doesn't know which user is about to login, it can't know for which user to get the value from the db.

What you need to do is store a file or something on the computer which contains the login data:

on startup
Check if user/pass file exists/has data
yes: load the data to the user/pass textboxes
no: do nothing

on login
Check if the user/pass is valid
yes: Check if remember checkbox is checked
     yes: save user/pass to file
     no: delete/clear file
no: delete/clear file



If you do not want a file where every user can see the login data, use a hash.

This post has been edited by elbielefeld: 29 May 2010 - 05:26 PM

Was This Post Helpful? 1
  • +
  • -

#3 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: how to remember password using checkbox ?

Posted 29 May 2010 - 11:12 PM

By using a Hash elbielefeld was trying to say that you should encrypt your password and store this encrypted value in database. This is the most common way of doing this and most of the applications and web pages do it. There is System.Security.Cryptography namespace where you will find all the lovely stuff for working with encryption. I suggest you browse this forum's tutorial section where you will find numerous tutorials on how to encrypt your password.


The basic idea is:

1. When new user is created, before you save his username and password, convert the password to Hash using one of the encryption algorithms and save this encrypted value to database. This encryption does not work in both ways, which means it is 'impossible' to decrypt the encrypted password.


2. User wants to log ==> User enters his password ==> encrypt the password with same Hash algorithm ==> compare this Hash value with the one stored in your database ==> if they match, user can login, otherwise not.


If you need any additional help, feel free to ask.
Was This Post Helpful? 0
  • +
  • -

#4 rabmat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-May 10

Re: how to remember password using checkbox ?

Posted 30 May 2010 - 12:44 AM

but still the issue how to save checkbox status
Was This Post Helpful? 0
  • +
  • -

#5 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: how to remember password using checkbox ?

Posted 30 May 2010 - 12:54 AM

Could you post your code inside this thread (not attaching it). Wrap you code inside code tags by clicking the '<>' button.
Was This Post Helpful? 0
  • +
  • -

#6 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: how to remember password using checkbox ?

Posted 30 May 2010 - 01:21 AM

As it has already been said. If a user puts a thick in a checkbox, write it's username in a text file. Then next time the user runs the application, read this text file. If file exists, read it's content and if you find any username stored in it, then log this user, in any other cases, user must provide its username and password in order to be able to log. Something like this...
Was This Post Helpful? 1
  • +
  • -

#7 rabmat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-May 10

Re: how to remember password using checkbox ?

Posted 30 May 2010 - 02:30 AM

Login Form code :
public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }
        private string un = "";
        private string pass = "";
        int chk = 0;
        object[] results = new object[3];
        private string connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;

        private void btnGo_Click(object sender, EventArgs e)
        {

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                // string query = "select * from Login";

                using (SqlCommand command = new SqlCommand("select * from Login", connection))
                {
                    command.CommandType = CommandType.Text;

                    SqlDataReader reader = command.ExecuteReader();
                    reader.Read();
                    reader.GetValues(results);
                    reader.Close();
                    un = results[0].ToString();
                    pass = results[1].ToString();
                    chk = (int)results[2];
                                      
                }
            }

            if ((txtUserName.Text == un ) && (TxtPassword.Text == pass))
            {
                this.Hide();
                Management newMan = new Management();
                newMan.ShowDialog();
                this.Close();
            }
            else
            {
                MessageBox.Show("Login failed, user name or password wrong.","Login failed",MessageBoxButtons.OK);
            }
                     
        }

          private void btnChange_Click(object sender, EventArgs e)
        {
            this.Hide();
            ChangePassword newChangePass = new ChangePassword();
            newChangePass.ShowDialog();
            this.ShowDialog();
                       
        }

     }        
  }     


Change password code :

 public partial class ChangePassword : Form
    {
        public ChangePassword()
        {
            InitializeComponent();
        }
        private string un="";
        private string pass="";
        private int chk;
        object[] results = new object[3];
        private string connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
        
        private void btnChangePass_Click(object sender, EventArgs e)
        {
                       
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                
                using (SqlCommand command = new SqlCommand("select * from Login", connection))
                {
                    command.CommandType = CommandType.Text;

                    SqlDataReader reader = command.ExecuteReader();
                    reader.Read();
                    reader.GetValues(results);
                    reader.Close();
                    un = results[0].ToString();
                    pass = results[1].ToString();
                    chk = (int)results[2];

                    if ((txtname.Text == un) && (txtOldPass.Text == pass) && (txtNewPass.Text == txtRetypePass.Text))
                    {
                        using (SqlCommand command2 = new SqlCommand())
                        {
                            command2.Connection = connection;
                            command2.CommandType = CommandType.Text;
                            command2.CommandText = "update Login set Password = @Newpass where UName = 'r'";
                            command2.Parameters.Add("@Newpass", SqlDbType.NVarChar, 20).Value = txtNewPass.Text;
                            command2.ExecuteNonQuery();
                            MessageBox.Show("Password changed successfully","Password changed");
                            this.Close();
                        }

                    }
                    else if (txtname.Text != un)
                    {
                        MessageBox.Show("Wrong user name", "Wrong username"); 
                    }
                    else if(txtOldPass.Text != pass)
                    {
                        MessageBox.Show("Wrong Old password supplied","please insert your old password");
                    }
                    else if(txtNewPass.Text != txtRetypePass.Text)
                    {
                     MessageBox.Show("The new password do not match, please retype the two new password fields","Matching error");
                    }

                }
            }
                      
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#8 rabmat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-May 10

Re: how to remember password using checkbox ?

Posted 30 May 2010 - 03:08 AM

is this task considered pro job or ordinary person can do it ?
Was This Post Helpful? 0
  • +
  • -

#9 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: how to remember password using checkbox ?

Posted 30 May 2010 - 03:39 AM

This is more or less a sample code, but should be enough to get you started...

using System;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

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

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string username = string.Empty;
            string password = string.Empty;
            string hashedPassword = string.Empty;


            if (chkRememberPassword.Checked)
            {
                username = m_test_username;
                hashedPassword = m_test_hashed_password;
            }
            else
            {
                username = txtUsername.Text.Trim();
                password = txtPassword.Text.Trim();

                if (string.IsNullOrEmpty(username))
                {
                    MessageBox.Show("Enter username");
                    return;
                }

                if (string.IsNullOrEmpty(password))
                {
                    MessageBox.Show("Enter password");
                    return;
                }

                hashedPassword = ComputeHash(password);
            }


            if (username == m_test_username && hashedPassword == m_test_hashed_password)
            {
                MessageBox.Show("Login successful!");
            }
            else
            {
                MessageBox.Show("Provided username/password is invalid.");
            }
        }

        public string ComputeHash(string text)
        {
            if (string.IsNullOrEmpty(text)) return null;
            if (m_crypto_provider == null) m_crypto_provider = new SHA256CryptoServiceProvider();

            byte[] tmpSource;
            byte[] tmpHash;

            tmpSource = ASCIIEncoding.ASCII.GetBytes(text);
            tmpHash = m_crypto_provider.ComputeHash(tmpSource);
            return Convert.ToBase64String(tmpHash);
        }


        private SHA256CryptoServiceProvider m_crypto_provider = null;

        //This is a test account - this data should be stored in database.
        private string m_test_username = "John";
        private string m_test_password = "Doe";
        private string m_test_hashed_password = "/VPvg1sVSFVypugs9HDctB/SGK5XUat1MclWoqa808c=";
    }
}


Was This Post Helpful? 2
  • +
  • -

#10 rabmat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-May 10

Re: how to remember password using checkbox ?

Posted 30 May 2010 - 03:50 AM

Thanks man.
i will do my best to solve this issue
i will be back to you if any help needed :)
thanks again.
Was This Post Helpful? 0
  • +
  • -

#11 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: how to remember password using checkbox ?

Posted 30 May 2010 - 03:55 AM

No problem... That's why we're here :-) Don't forget to click the '+' button in the bottom-right corner of user's post to say thanks if the post was helpful.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1