hi

i am creating a login page in C#.net windows application and i have a remember me checkbox in the login interface. can some one give me a tutorial to code the remember me check box in the C# windows application

thank you

If you have a login, I assume you use a database, where you save users name and passwords (and other informations).
If so, you can create a new column in the Users table, which will save the information about "saving user`s name". You need only two states - true, or false - to remember username or not. But because dataBase does not have boolean type, you can choose bit. Bit only knows two states 0 or 1.
So what to do:
When user addes a tick to remember his userName, you will update this column to 1.
And when the user will next time want to login, the code will do the select statement of this column, and if the value is 1, the userName 8and password if you want) will be retreived out of the dataBase, and written into appropriate textBoxes, and the tick will be added.
This is it - its simple.

at this point login/password would have not been validated so you can't really retrieve this config from the db.. if it were some other settings I would agree, but with this sort of setting, you can't use the database.

Iam not really sure what he meant with "remember me checkbox", so its hard to tell what exactly he wants.
I would need some better explanation in the 1st place... so please...

yea you're right.. however since the topic is login form and "remember me" im assuming a checkbox is required to remember login info which cant be populated via a db :)

Iam not really sure what he meant with "remember me checkbox", so its hard to tell what exactly he wants.
I would need some better explanation in the 1st place... so please...

remember me check box -
if you check the remeber me password the next time the user comes he does not have to type in the user name and the password.

If you have a login, I assume you use a database, where you save users name and passwords (and other informations).
If so, you can create a new column in the Users table, which will save the information about "saving user`s name". You need only two states - true, or false - to remember username or not. But because dataBase does not have boolean type, you can choose bit. Bit only knows two states 0 or 1.
So what to do:
When user addes a tick to remember his userName, you will update this column to 1.
And when the user will next time want to login, the code will do the select statement of this column, and if the value is 1, the userName 8and password if you want) will be retreived out of the dataBase, and written into appropriate textBoxes, and the tick will be added.
This is it - its simple.

this ia na good somple idea
but how does the application knows which user wants to login

this ia na good somple idea
but how does the application knows which user wants to login

Which?
Simple.
The one with the enters userName. You said you want to remember his password. So in the TextChnaged event (when user will write his username letter by letter), your code will constantly look for the match. If it will be found, on the Enter key press or TAB password will appear in the password textBox, and tick will be added to checkBox (and login will be available). Simple!

Remember jusr one thing: this will work, if your userName column in the dataBase will be UNIQUE!!! It must be, there must be NO duplications. This is a condition no.1.

There is some other way instead of constantly checking dataBase, before user starts entering lettes, go to dataBase, get all userNames and passwords out into DataTable!!! (fill it will sqlDataAdapter), and then you only have to compare the inserted username with the column in the DataTable (no DataBasE).

For the rest of the info, please refer to my 1st post in this thread!! its all there.

Which?
Simple.
The one with the enters userName. You said you want to remember his password. So in the TextChnaged event (when user will write his username letter by letter), your code will constantly look for the match. If it will be found, on the Enter key press or TAB password will appear in the password textBox, and tick will be added to checkBox (and login will be available). Simple!

Remember jusr one thing: this will work, if your userName column in the dataBase will be UNIQUE!!! It must be, there must be NO duplications. This is a condition no.1.

There is some other way instead of constantly checking dataBase, before user starts entering lettes, go to dataBase, get all userNames and passwords out into DataTable!!! (fill it will sqlDataAdapter), and then you only have to compare the inserted username with the column in the DataTable (no DataBasE).

For the rest of the info, please refer to my 1st post in this thread!! its all there.

i wanted to remeber the useranme and the password both
but not just only the password

is there a solution to this

I already explanined you in my 1st post, there I was telling you how to save both; username and password.
READ carefully my 1st post, there is explained all - of how to create a new column (type of bit), and how the bit to 1 when user adds a tick (when bit set to 1, that means when you will read username and password, the password will be shown automatically).

Let me show you an example!

DB creation: Table USERS:
Columns: Id(int), UserName(varchar,50), Password(varchar,50), ShowPassword(bit) (and maybe some more, but these are obligatory)
By default "ShowPasswrd" column is set to 0 (when 0 means no password shown)

On Login form:
For the 1st time, when user logs in, there is no chance of automatically showing the password, because this time its the 1st time when the user can add a tick, which will mean that when he will try to login next times, the auto-login will be available.

So, now he enters his data (username and password) and adds tick in the checkBox).
What happens now:

//On buttonLogin click event:
//check users validation`s data!
//and change the bit value to 1 (when 1 mean password will be nextimes shown automatically)

private void SaveAutoLogin(string userName)
{
    using(SqlConnection sqlConn = new SqlConnection(@"connString"))
    {
         string sqlQuery = @"UPDATE Users SET ShowPassword = @show WHERE UserName = @user";
         using(SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn))
         {
               smd.Parameters.Add("@show", SqlDbType.Bit).Value = 1;
               cmd.Parameters.Add("@user", SqlDbType.Varchar, 50).Value = userName;
               cmd.ExecuteNonQuery();
         }
    }
}

Now this is set. When the user will come next time you will check the "ShowPassword" status, and if true, the password will be shown automatically (in runtime by code), if false, use will have to insert his password manually (by typing).
To sheck you do:

//on login form:
private void textBoxUserName_TextChanged(object sender, EventArgs e)
{
    bool bAutoShowPassword = CheckingForAutoLogin(textBoxUserName.Text.Trim());
    if(bAutoShowPassword)
    {
        //auto login is available, so you do:
        checkbox1.Checked = true;
        textBoxPassword.Text = GetUserPassword(textBoxUserName.Text.Trim());
        //set password to some other character, to make text not readable:
        textBoxPassword.PasswordChar = '*';
        //and if there is something else to do!!
        //but now it all set to login in correctly!
    }
}
public bool CheckingForAutoLogin(stirng userName)
{
    bool bAutoLogin = false;
    using(SqlConnection sqlConn = new SqlConnection(@"connString"))
    {
         string sqlQuery = @"SELECT ShowPassword FROM Users WHERE UserName = @user";
         using(SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn))
         {
               cmd.Parameters.Add("@user", SqlDbType.Varchar, 50).Value = userName;
               using(SqlDataReader reader = cmd.ExecuteReader())
               {
                    if(read.Read())
                          bAutoLogin = ((int)reader[0]== 1 ? true : false;
                          //this code returns true if value is 1, else returns false
               }
         }
    }
    return bAutoLogin;
}

public string GetUserPassword(stirng userName)
{
    string pswd = null;
    using(SqlConnection sqlConn = new SqlConnection(@"connString"))
    {
         string sqlQuery = @"SELECT Password FROM Users WHERE UserName = @user";
         using(SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn))
         {
               cmd.Parameters.Add("@user", SqlDbType.Varchar, 50).Value = userName;
               using(SqlDataReader reader = cmd.ExecuteReader())
               {
                    if(read.Read())
                          pswd = (string)reader[0];
               }
         }
    }
    return pswd;
}

This is about it. You see its not that difficult.
Remember: you have to imagine all the picture whats will be going on, when user will try to login, I am to imagine all STEPS (from beginning to the end), and step by step, and then you simple do the code, which comes at a particular step!

I hope I helped you a bit, and please, read the code and manual carefully, and use the code I gave you here. Its almost all you need!!
And just a bit of imagination is always welcome!

bye, :)

Edited 5 Years Ago by Mitja Bonca: n/a

I already explanined you in my 1st post, there I was telling you how to save both; username and password.
READ carefully my 1st post, there is explained all - of how to create a new column (type of bit), and how the bit to 1 when user adds a tick (when bit set to 1, that means when you will read username and password, the password will be shown automatically).

Let me show you an example!

DB creation: Table USERS:
Columns: Id(int), UserName(varchar,50), Password(varchar,50), ShowPassword(bit) (and maybe some more, but these are obligatory)
By default "ShowPasswrd" column is set to 0 (when 0 means no password shown)

On Login form:
For the 1st time, when user logs in, there is no chance of automatically showing the password, because this time its the 1st time when the user can add a tick, which will mean that when he will try to login next times, the auto-login will be available.

So, now he enters his data (username and password) and adds tick in the checkBox).
What happens now:

//On buttonLogin click event:
//check users validation`s data!
//and change the bit value to 1 (when 1 mean password will be nextimes shown automatically)

private void SaveAutoLogin(string userName)
{
    using(SqlConnection sqlConn = new SqlConnection(@"connString"))
    {
         string sqlQuery = @"UPDATE Users SET ShowPassword = @show WHERE UserName = @user";
         using(SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn))
         {
               smd.Parameters.Add("@show", SqlDbType.Bit).Value = 1;
               cmd.Parameters.Add("@user", SqlDbType.Varchar, 50).Value = userName;
               cmd.ExecuteNonQuery();
         }
    }
}

Now this is set. When the user will come next time you will check the "ShowPassword" status, and if true, the password will be shown automatically (in runtime by code), if false, use will have to insert his password manually (by typing).
To sheck you do:

//on login form:
private void textBoxUserName_TextChanged(object sender, EventArgs e)
{
    bool bAutoShowPassword = CheckingForAutoLogin(textBoxUserName.Text.Trim());
    if(bAutoShowPassword)
    {
        //auto login is available, so you do:
        checkbox1.Checked = true;
        textBoxPassword.Text = GetUserPassword(textBoxUserName.Text.Trim());
        //set password to some other character, to make text not readable:
        textBoxPassword.PasswordChar = '*';
        //and if there is something else to do!!
        //but now it all set to login in correctly!
    }
}
public bool CheckingForAutoLogin(stirng userName)
{
    bool bAutoLogin = false;
    using(SqlConnection sqlConn = new SqlConnection(@"connString"))
    {
         string sqlQuery = @"SELECT ShowPassword FROM Users WHERE UserName = @user";
         using(SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn))
         {
               cmd.Parameters.Add("@user", SqlDbType.Varchar, 50).Value = userName;
               using(SqlDataReader reader = cmd.ExecuteReader())
               {
                    if(read.Read())
                          bAutoLogin = ((int)reader[0]== 1 ? true : false;
                          //this code returns true if value is 1, else returns false
               }
         }
    }
    return bAutoLogin;
}

public string GetUserPassword(stirng userName)
{
    string pswd = null;
    using(SqlConnection sqlConn = new SqlConnection(@"connString"))
    {
         string sqlQuery = @"SELECT Password FROM Users WHERE UserName = @user";
         using(SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn))
         {
               cmd.Parameters.Add("@user", SqlDbType.Varchar, 50).Value = userName;
               using(SqlDataReader reader = cmd.ExecuteReader())
               {
                    if(read.Read())
                          pswd = (string)reader[0];
               }
         }
    }
    return pswd;
}

This is about it. You see its not that difficult.
Remember: you have to imagine all the picture whats will be going on, when user will try to login, I am to imagine all STEPS (from beginning to the end), and step by step, and then you simple do the code, which comes at a particular step!

I hope I helped you a bit, and please, read the code and manual carefully, and use the code I gave you here. Its almost all you need!!
And just a bit of imagination is always welcome!

bye, :)

you didn't reply to the post i have commented on your first poast i uderstood what you have said in the 2nd post.

in the second time the user have to type in te username to pop the password to the password field if the value of the DB is set to true.

i want the username also to be generated in the textbox username, but then how will the pplication know which user is going to log

Mitja Bonca:

so you're saying that once a user logs in for the first time and checks (accepts) for saving user name and password, the second time the user enters his USER name only, the password will appear automatically?

if this is the case, it is unheard of - it goes against any possible software security principle...

so basically if I get hold of this USER's username, i can basically login on his/her behalf as the password would have appeared automatically ...from any PC which has this app installed?

Edited 5 Years Ago by jfarrugia: n/a

Why is then "Remember my password" for? Its meant that one computer uses one single person. In case if there is more persons using one computer, and someone thinks there might come to the abuse of the logging in (using someone elses username and password), then uisng this option is out of the question.

But if he wants to use it, then this is the right approach. It is simple made to make it easier to the user (not to write username and password all the time he wants to login).

the idea is completely out of line and unfortunately your perception of this functionality is wrong Mitja... take GMAIL as an example, or any other system in this galaxy... If i enter your username on gmail.com, will it give me your password if you had opted to save username/password?? NO.. it will load my user name + password from an encrypted cookie stored locally....locally is the keyword - this means that one has to use his own PC, and that means that the option to store username + password must be localised and not retrieved from a server. there is no such implementation as you state.

in 12 years of development i have never heard of such an approach.

To the original poster: i gave you two options + links to relevant code, I am leaving this post as I've contributed :)

good luck and message if you have any issues in implementing the suggestions i gave you.

We came to the misunderstandning here... I know for the cookie that is created in the local computer, and it has to be (and it is) used on that computer only.
As said, if you remember the password, some else can easily login to gmail from your computer - this is what Im saysing, not like you thing I was thniking: that if you type my userName in gmail, it will give you mine data - No. I was never ever thinking of this kind of approach.

This article has been dead for over six months. Start a new discussion instead.