WEB Cookie
//写Cookie
protected void btnwrite_Click(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("Name");
cookie.Value = "张三";
cookie.Expires = DateTime.Now.AddDays(1);//设置过期时间为 1 天
Response.Cookies.Add(cookie);
}
//读Cookie
protected void btnread_Click(object sender, EventArgs e)
{
if (Request.Cookies["Name"] != null)
{
txtCookie.Text = Request.Cookies["Name"].Value;
}
else
{
txtCookie.Text = "没有找到哦";
}
}
//删除cookie
protected void btnDelete_Click(object sender, EventArgs e)
{
if (Request.Cookies.Count > 0)
{
for (int i = 0; i < 100; i++)
{
HttpCookie username = new HttpCookie("username" + i); username.Expires = DateTime.Now.AddDays(-2);
Response.Cookies.Add(username);
}
}
Response.Redirect("DeleteCookie.aspx");
}
//实现记住我功能 或 自动登录(有加密解密)
<body>
<form id="form1" runat="server">
<div>
用户名:<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<br />
密码:<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
<br />
记住我: <asp:CheckBox ID="ckusername" runat="server" />
自动登录:<asp:CheckBox ID="cklogin" runat="server" />
<br />
<asp:Button ID="brnlogin" runat="server" Text="登录" οnclick="brnlogin_Click" />
</div>
</form>
</body>
后台
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//记住我
if (Request.Cookies["username"] != null)
{
txtUserName.Text = Decode(Request.Cookies["username"].Value, "Rainight");
}
//自动登录
if (Request.Cookies["username"] != null && Request.Cookies["password"] != null)
{
if (Request.Cookies["username"].Value == "admin" && Decode( Request.Cookies["password"].Value,"Rainight") == "123")
{
Response.Redirect("readandwrite.aspx");
}
}
}
}
protected void brnlogin_Click(object sender, EventArgs e)
{
if (ckusername.Checked == true)
{
cklogin.Enabled = false;
HttpCookie username = new HttpCookie("username");
username.Value = Encode(txtUserName.Text.Trim(), "Rainight");
username.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(username);
//
}
if (cklogin.Checked)
{
ckusername.Enabled = false;
HttpCookie username = new HttpCookie("username");
username.Value = txtUserName.Text.Trim();
username.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(username);
HttpCookie password = new HttpCookie("password");
password.Value =Encode( txtPassword.Text.Trim(),"Rainight");
password.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(password);
}
Response.Redirect("readandwrite.aspx");
}
//加密
public static string Encode(string str, string key)
{
try
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(str);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(bytes, 0, bytes.Length);
stream2.FlushFinalBlock();
StringBuilder builder = new StringBuilder();
foreach (byte num in stream.ToArray())
{
builder.AppendFormat("{0:X2}", num);
}
stream.Close();
return builder.ToString();
}
catch
{
return "不是用Encrypt加密的字符串,加密失败!";
}
}
//解密
public static string Decode(string str, string key)
{
try
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
byte[] buffer = new byte[str.Length / 2];
for (int i = 0; i < (str.Length / 2); i++)
{
int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10);
buffer[i] = (byte)num2;
}
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
stream2.Write(buffer, 0, buffer.Length);
stream2.FlushFinalBlock();
stream.Close();
return Encoding.GetEncoding("GB2312").GetString(stream.ToArray());
}
catch
{
return "不是用Encrypt加密的字符串,解密失败!";
}
}