Cookie的运用

本文介绍了如何使用Cookie对象来保存和读取客户端信息,包括获取客户端IP地址并将其保存在Cookie中,以及如何设置Cookie的有效期,并通过实例展示了如何截取登录名并将其作为Cookie值进行传递。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cookie的运用
   1.使用Cookie对象保存和读取客户端信息
string UserIP = Reuqest.UserHostAddress.ToString();//获取客户端的IP地址
Response.Cookies["IP"].Value = UserIP; //将客户端的IP地址保存在Cookies对象中
Response.Cookies["IP"].Expires = DataTime.MaxValue; //设计Cookies的失效期

Response.Write(Request.Cookies["IP"].Value); //从Cookies中读取客户端IP地址值


-->string LOGON_USER = Request.ServerVariables["LOGIN_USER"].Trim();   //获取登录名 QCS./Michael.Cheng
   LOGON_USER=LOGON_USER.Substring(LOGON_USER.IndexOf("//")+1,LOGON_USER.Length-LOGON_USER.IndexOf("//")-1);   //截取,Michael.Cheng
   HttpCookie Cookie = new HttpCookies("OAM");        //新建OAM 的Cookie
   Response.Cookies["OAM"].Expires = new System.DateTime(2015,8,20);     //设置Cookie的有效期到2015年8月20日
  
   Cookie.Values.Add("OAMUserName",LOGON_USER);    //把登录名赋值给OAMUserName
   //Response.Cookies["OAMUserName"].Value = LOGON_USER;  //效果同上
   Response.Cookies.Add(Cookie);
   Response.Redirect("Menu/**.Asp");

   **.Asp
   If session("login_user_id") ="" then
 Dim ck
 Set ck = Request.Cookies("OAM");
If Not (ck is Nothing) then
Session("login_user") =ck("OAMUserName")
          End If
    End If

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值