0

我有一个登录表单,我想制作一个带有记住我功能的复选框......谁能给我一些如何制作它的代码?我没有尝试任何东西,因为我不知道..

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

3 回答 3

2

使用以下代码创建 Cookie

Response.Cookies("userName").Value = "mike"
Response.Cookies("userName").Expires = DateTime.Now.AddDays(1)
于 2013-06-09T09:55:02.067 回答
0

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

于 2013-06-09T09:48:08.870 回答
0

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

public string login(string username, string password)// 登录方法 { 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;
    }
于 2013-12-03T19:12:20.660 回答