Robert Vlasiu
Robert Vlasiu

Reputation: 402

C# Creating A Remember me

I have a Log in form and i want to make a check box with a Remember Me Function...Can anyone give me some codes of how to make it? I didn't try anything because i have no idea ..

    int timeout = rememberMe ? 525600 : 30; // Timeout in minutes, 525600 = 365 days.
    var ticket = new FormsAuthenticationTicket(userName, rememberMe, timeout);
    string encrypted = FormsAuthentication.Encrypt(ticket);
    var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
    cookie.Expires = System.DateTime.Now.AddMinutes(timeout);// Not my line
    cookie.HttpOnly = true; // cookie not available in javascript.
    Response.Cookies.Add(cookie);

I found this but i don;t know where to put it ..

The rest of the code is:

using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM members WHERE username = '" + textBox2.Text + "' AND password = '" + textBox3.Text + "';"))
                {
                    cmd.Connection = con;

                    DataTable dt = new DataTable();
                    con.Open();
                    MySqlDataReader reader = cmd.ExecuteReader();

                    if (reader.Read() != false)
                    {
                        if (reader.IsDBNull(0) == true)
                        {
                            cmd.Connection.Close();
                            reader.Dispose();
                            cmd.Dispose();
                            MessageBox.Show("Oops!There was a problem!");
                        }
                        else
                        {
                            cmd.Connection.Close();
                            reader.Dispose();
                            cmd.Dispose();
                            this.Hide();

                           }
                    }

                    else
                    {
                        MessageBox.Show("You Login Information is incorrect!");
                    }

Upvotes: 0

Views: 1852

Answers (3)

user3062716
user3062716

Reputation: 19

Loging in Difeerent users from different tables

public string login(string username, string password)// login method { dataBaseObj.sqlconn.Open();

        dataBaseObj.sqlCmd = new SqlCommand("SELECT  clEmail, clPassword FROM Client where clEmail='" + username + "'", dataBaseObj.sqlconn);
        dataBaseObj.sqlDr = dataBaseObj.sqlCmd.ExecuteReader();


        if (dataBaseObj.sqlDr.Read())
        {
            if (dataBaseObj.sqlDr["clPassword"].Equals(password.ToString()))
            {
                strmessage = "client";
                dataBaseObj.sqlconn.Close();
            }
            else
            {
                intResult++;

                strmessage = "login  not succesfull";

                dataBaseObj.sqlconn.Close();

                if (intResult == 3)
                {
                    strmessage = "your Blocked";
                }

            }
        }    // if its not the client is gonna go to workers table 
        else
        {
            dataBaseObj.sqlCmd = new SqlCommand("SELECT wuUsername, wuPassword, wuUserType FROM  WorkUsers where wuUsername'" + username + "'", dataBaseObj.sqlconn);
            dataBaseObj.sqlDr = dataBaseObj.sqlCmd.ExecuteReader();

            if (dataBaseObj.sqlDr.Read())
            {
                if (dataBaseObj.sqlDr["wuPassword"].Equals(password.ToString()))
                {
                    strmessage = "Receptionist";
                    dataBaseObj.sqlconn.Close();
                }
                else
                {

                    intResult++;

                    strmessage = "login  not succesful";

                    dataBaseObj.sqlconn.Close();

                    if (intResult == 3)
                    {
                        strmessage = "your Blocked";
                    }
                }
            }
        }
        return strmessage;
    }

Upvotes: 0

Fakhar uz Zaman
Fakhar uz Zaman

Reputation: 299

Use following code to create Cookies

Response.Cookies("userName").Value = "mike"
Response.Cookies("userName").Expires = DateTime.Now.AddDays(1)

Upvotes: 2

Christian Phillips
Christian Phillips

Reputation: 18769

Take a look at this link on Forms Authentication, http://msdn.microsoft.com/en-us/library/ff647070.aspx. This code would be within the method/action which handles the 'Login' button click, after the user supplies their login credentials.

Upvotes: 0

Related Questions