一、Cookie对象
1.定义:Cookie对象用于保存客户端浏览器请求的服务器页面,也可用于存放非敏感性的用户信息,信息保存的时间可以根据用户的需要进行设置。并非所有的浏览器都支持Cookie,并且数据信息是以文本的形式保存在客户端的计算机中。
2.常用属性
Cookie对象的常用属性 属性说明Expires设定Cookie变量的有效时间,默认为1000分钟。若设为0,则可以实时删除Cookie变量Name取得Cookie变量的名称Value获取或设置Cookie变量的内容值Path获取或设置Cookie适用的URL3.常用方法
Cookie对象的常用方法 方法说明Equals确定指定Cookie是否等于当前的CookieToString返回此Cookie对象的一个字符串表示形式4.实例
(1)用Cookie读取客户端信息
代码
<form id="form1" runat="server"> <div> <asp:Button ID="btnWrite" runat="server" OnClick="btnWrite_Click" Text="将用户IP写入Cookie" Width="146px" /><br /> <asp:Button ID="btnRead" runat="server" OnClick="btnRead_Click" Text="将用户IP从Cookie中读出" Width="147px" /><br /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form> protected void Page_Load(object sender, EventArgs e) { } protected void btnWrite_Click(object sender, EventArgs e) { string UserIP = Request.UserHostAddress.ToString(); Response.Cookies["IP"].Value = UserIP; } protected void btnRead_Click(object sender, EventArgs e) { this.Label1.Text = Request.Cookies["IP"].Value; }结果
(2)对Cookie中的数据加密
string data = "对Cookie中的数据加密"; Response.Cookies["data"].Value = Forms.Authentication.HashPasswordForStoringInConfigFile(data,"md5");
转载于:https://www.cnblogs.com/bosamvs/p/5712627.html