遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

Cookie概要

 
  以下内容适合IE6.0浏览器

  1. Cookie用来保存信息,是Session,Asp.net Form认证的基础


  2.Cookie 有 Name, Value,Domain, Expires,Path,HttpOnly等属性


  3. Cookie 按存在时间分临时Cookie,跟持久Cookie.
   临时Cookie没指明Expires属性,因此不会在IE的临时文件夹中生成相应cookie文件,而持久cookie是指设置了expires属性,(具体时间可以是比当前时间多1秒或者2秒...,不一定是很长的时间),这样的cookie会在IE临时文件夹下生成一个cookie文件,当然这个cookie文件会在expires到期后自动删除掉.

  4. IE6.0支持Cookie HttpOnly 属性,指定了HttpOnly属性的cookie 不允许客户端脚本访问如: URL(地址栏)中输入javascript:alert(document.cookie) 将显示含HttpOnly属性的cookie,当然着也意味着javascript无法生成含httpOnly的cookie, 这样可以在一定程度上避免跨站点脚本攻击.
Asp.net2.0以及以后版本中的HttpCookie类可以直接设置HttpOnly属性,不过Asp中你需要类似下面的处理:

-------代码--------------

<%
'**************************************************
'ASP 中输出httponly cookie IE6.0以上浏览器支持
'WDFrog
'2009-04-15
'<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
'**************************************************

'----------SetHttpOnlyCookie----------------------------------------
'功能:设置HttpOnly Cookie
'参数:expDate 为保到期, 0表示不设置,设置为过去某一时间表示清除
'参数:domain 为空(string.Empty)表示不设置
'-------------------------------------------------------------------
Function SetHttpOnlyCookie(cookieName,cookieValue,domain,path,expDate)
  Dim cookie
  cookie=cookieName & "=" & Server.URLEncode(cookieValue) & "; path=" & path
  If expDate <> 0 Then
    cookie=cookie & "; expires=" & DateToGMT(expDate)
  End If
 
  If domain <> "" Then
    cookie=cookie & "; domain=" & domain
  End If

  cookie=cookie & "; HttpOnly"
 
  Call Response.AddHeader ("Set-Cookie", cookie)
End Function

'-------------getGMTTime------------
'参数: sDate 需要转换成GMT的时间
'---------------------------------
Function DateToGMT(sDate)
   Dim dWeek,dMonth
   Dim strZero,strZone
   strZero="00"
   strZone="+0800"
   dWeek=Array("Sun","Mon","Tue","Wes","Thu","Fri","Sat")
   dMonth=Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
   DateToGMT = dWeek(WeekDay(sDate)-1)&", "&Right(strZero&Day(sDate),2)&" "&dMonth(Month(sDate)-1)&" "&Year(sDate)&" "&Right(strZero&Hour(sDate),2)&":"&Right(strZero&Minute(sDate),2)&":"&Right(strZero&Second(sDate),2)&" "&strZone
End Function
'参考
'Call SetHttpOnlyCookie("cookieOnly1","onlyValue",".gyzs.com","/",0)
 
%>

 

参考文章:
http://www.asp101.com/tips/index.asp?id=160

posted on   遗忘海岸  阅读(410)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
点击右上角即可分享
微信分享提示