C#创建A记住我

时间:2013-06-09 09:40:00

标签: c# forms login

我有一个登录表单,我想制作一个带有记住我功能的复选框...任何人都可以给我一些如何制作它的代码吗? 我没有尝试任何事情,因为我不知道..

    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);

我找到了这个,但我不知道该把它放在哪里..

其余代码是:

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!");
                    }

3 个答案:

答案 0 :(得分:2)

使用以下代码创建Cookie

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

答案 1 :(得分:0)

请查看表单身份验证上的此链接http://msdn.microsoft.com/en-us/library/ff647070.aspx。在用户提供登录凭据后,此代码将位于处理“登录”按钮单击的方法/操作中。

答案 2 :(得分:0)

从不同的表中登录不同的用户

public string login(string username,string password)//登录方法{dataBase Obj.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;
    }