错误是:当我尝试使用 Response.Cookies.Add(cookie); 时,当前上下文中不存在名称“响应”;
我不知道我还需要包括什么才能让它工作。我制作了用户可以登录的应用程序,并在他们登录时制作了 cookie,因此他们在重新打开应用程序时不需要再次登录。
using MySql.Data.MySqlClient;
using System.Web;
namespace login
{
public partial class Form1 : Form
{
MySqlConnection konekcija;
string baza = "host=localhost;database=test;user=root;password=";
MySqlCommand comm;
MySqlDataReader reader;
HttpCookie cookie;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
konekcija = new MySqlConnection(baza);
comm = konekcija.CreateCommand();
konekcija.Open();
}
private void button1_Click(object sender, EventArgs e)
{
string user = textBox2.Text.ToString();
string pass = textBox1.Text.ToString();
trylogin( user, pass);
}
public void trylogin(string user, string pass)
{
if (checkBox1.Checked)
{
cookie = new HttpCookie("remember_me");
cookie["Username"] = textBox2.Text;
cookie["Expire"] = "365 Days";
cookie.Expires = DateTime.Now.AddDays(365);
Response.Cookies.Add(cookie);
}
comm.CommandText = "SELECT * FROM korisnici WHERE user='"+user+"' AND pass='"+pass+"'";
reader = comm.ExecuteReader();
if (reader.Read() == true)
{
reader.Dispose();
}
else
{
reader.Dispose();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
konekcija.Close();
}
}
}