经典 Asp cookie 的过期日期并不总是被设置

M.Y*_*.Y. 4 vbscript asp-classic

我正在尝试在 Classic Asp 中使用 addheader -method 设置 cookie,这是向 cookie 添加 HttpOnly 和 Secure -flags 的唯一方法。所有这些都适用于以下代码 - 但有一个例外,它是到期日期/时间。

<%
Response.AddHeader "Set-Cookie", "testCookie=2000; path=/;HttpOnly;Secure;expires=" & dateAdd("d", 365, Now()) & ";samesite=Strict;HostOnly"
%>
Run Code Online (Sandbox Code Playgroud)

不过,这似乎是与浏览器相关的问题。在 Firefox 中,我可以在开发人员工具的“存储”选项卡中看到设置了过期时间。但在 Chrome 中,它始终保持默认状态,即会话结束时过期。Edge 也存在同样的问题。

有人对这个问题有经验吗?

Kul*_*gin 5

此处记录了预期的日期格式。您需要以这种方式生成到期日期。

在经典 ASP 中,您可以使用服务器端 JavaScript 轻松生成此类日期。

<!--#include file="HTTPDate.asp"-->
<%
Response.AddHeader "Set-Cookie", "testCookie=2000; path=/;HttpOnly;Secure;expires=" & HTTPDate(DateAdd("d", 365, Now())) & ";samesite=Strict;HostOnly"
%>
Run Code Online (Sandbox Code Playgroud)

HTTP日期.asp

<script language="javascript" runat="server">
function HTTPDate(vbsDate){
    return (new Date(vbsDate)).toGMTString().replace(/UTC/, "GMT");
}
</script>
Run Code Online (Sandbox Code Playgroud)

编辑:添加纯 VBScript 解决方案。

<%
Function CurrentTZO()
    With CreateObject("WScript.Shell") 
        CurrentTZO = .RegRead( _ 
        "HKLM\System\CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias")
    End With
End Function

Function Pad(text)
    Pad = Right("00" & text, 2)
End Function

Function HTTPDate(ByVal localDate)
    localDate = DateAdd("n", CurrentTZO(), localDate)

    ' WeekdayName and MonthName functions relies on locale
    ' need to produce day and month name abbreviations in en-US locale
    Dim locale : locale = SetLocale("en-US")

    Dim out(5)
    out(0) = WeekdayName(Weekday(localDate), True) & ","
    out(1) = Pad(Day(localDate))
    out(2) = MonthName(Month(localDate), True)
    out(3) = Year(localDate)
    out(4) = Join(Array(Pad(Hour(localDate)), Pad(Minute(localDate)), Pad(Second(localDate))), ":")
    out(5) = "GMT"

    SetLocale locale ' set original locale back 

    HTTPDate = Join(out, " ")
End Function

Response.AddHeader "Set-Cookie", "testCookie=2000; path=/;HttpOnly;Secure;expires=" & HTTPDate(DateAdd("d", 365, Now())) & ";samesite=Strict;HostOnly"
%>
Run Code Online (Sandbox Code Playgroud)