创建cookie

cookie的创建
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AspDotNet04
{
public partial class C10CookieLogin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.HttpMethod.ToLower() == "post")
{
string strN = Request.Form["txtN"];
string strP = Request.Form["txtP"];
//登录成功
if (strN == "admin" && strP == "123")
{
//创建Cookie,存入 键值对
HttpCookie cookie = new HttpCookie("cname",strN);
//设置失效时间(硬盘Cookie) - 2分钟
cookie.Expires = DateTime.Now.AddMinutes(2);
//设置cookie的Path
//cookie.Path = "/admin/";
//cookie.Domain = "www.baidu.com";
//加入响应报文对象
Response.Cookies.Add(cookie);

//让浏览器 重定向 到首页
Response.Redirect("C11CookieIndex.aspx");
//Response.Redirect("/admin/CookieIndex.aspx");
}
}
}
}
}

原文地址:https://www.cnblogs.com/kexb/p/3660397.html