Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript IIS web服务器上的UTC日期时间与浏览器端的UTC时间不匹配_Javascript_C#_Asp.net_Date_Forms Authentication - Fatal编程技术网

Javascript IIS web服务器上的UTC日期时间与浏览器端的UTC时间不匹配

Javascript IIS web服务器上的UTC日期时间与浏览器端的UTC时间不匹配,javascript,c#,asp.net,date,forms-authentication,Javascript,C#,Asp.net,Date,Forms Authentication,我有一个ASP.Net应用程序托管在一个流行的ASP.Net托管提供商上,我将表单身份验证过期日期时间存储在一个cookie中。当请求几乎同时返回到客户端/浏览器时,我使用下面的代码确定cookie中与此UTC日期时间对应的本地时间。我在上面的cookie中存储年、月、日、小时、分钟、秒和毫秒,而不是日期时间的字符串值 JavaScript代码 我使用的表单身份验证超时为4分钟。由于请求几乎立即返回,因此在上述代码中,日期应比cDate晚4分钟,但总是比预期的晚1分钟,即晚3分钟。即使我将表单身

我有一个ASP.Net应用程序托管在一个流行的ASP.Net托管提供商上,我将表单身份验证过期日期时间存储在一个cookie中。当请求几乎同时返回到客户端/浏览器
时,我使用下面的代码确定cookie中与此UTC日期时间对应的本地时间。我在上面的cookie中存储年、月、日、小时、分钟、秒和毫秒,而不是日期时间的字符串值

JavaScript代码

我使用的表单身份验证超时为4分钟。由于请求几乎立即返回,因此在上述代码中,日期应比cDate晚4分钟,但总是比预期的晚1分钟,即晚3分钟。即使我将表单身份验证超时值设置为4分钟以外的值,我仍然发现与
cDate
相比,
nowDate
总是比预期值少一分钟

问题:即使请求几乎是瞬时的,这种奇怪的时差的原因是什么?我找不到任何可能的原因,除了web服务器可能有一些不正确的时间设置。我尝试了表单身份验证超时值的不同值,但差异始终为1分钟

以UTC为单位存储票据到期的C#代码

表单身份验证配置



您还需要将客户端日期设置为UTC-@jeff,如果您在JavaScript下查看我的代码,您会注意到我正在使用date.UTC函数,该函数返回UTC日期自1970-1-1以来的毫秒数。因此,服务器端的UTC日期被正确地转换为浏览器端的本地日期。@jeff,我可以将服务器端的UTC日期与浏览器UTC日期进行比较,或者将服务器端转换为本地浏览器日期的UTC日期与浏览本地日期进行比较。我使用的是后者,所以这不重要。如果差异始终为1分钟,那么服务器时间不正确的可能性较小。即使它不正确,当您更改超时时,差异也应该更改。是否可能根本没有使用超时?如果怀疑服务器时间不正确,可以使用chrome或首选浏览器从响应头检查服务器时间,并将其与客户端计算机时间进行比较。
var c = getCookie("xyz");//getCookie is a custom function for getting cookie value
if (c) {
var dateArray = c.split(",");
//cDate is local date time on browser for server-side UTC date time
var cDate = new Date(Date.UTC(dateArray[0], dateArray[1], dateArray[2],
                          dateArray[3], dateArray[4], dateArray[5], dateArray[6]));
 //nowDate is current local date time on browser side
 var nowDate = new Date();//this should be very close to 4 minutes behind cDate
}
 void Application_AuthenticateRequest(object sender, EventArgs e)
{
    if (Context.Request.IsAuthenticated && Context.User.Identity.AuthenticationType == "Forms")
    {
        FormsIdentity identity = (FormsIdentity)Context.User.Identity;
        DateTime utcDateTime = identity.Ticket.Expiration.ToUniversalTime();
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append(utcDateTime.Year);
        sb.Append(",");
        sb.Append(utcDateTime.Month - 1);//since JavaScript uses a 0 based index for month
        sb.Append(",");
        sb.Append(utcDateTime.Day);
        sb.Append(",");
        sb.Append(utcDateTime.Hour);
        sb.Append(",");
        sb.Append(utcDateTime.Minute);
        sb.Append(",");
        sb.Append(utcDateTime.Second);
        sb.Append(",");
        sb.Append(utcDateTime.Millisecond);
        HttpCookie cookie = new HttpCookie("xyz", sb.ToString());
        Response.Cookies.Add(cookie);
    }
}
 <authentication mode="Forms">
      <forms cookieless="UseCookies" loginUrl="~/Login.aspx" name="ABCAuth" 
       protection="All" requireSSL="false" slidingExpiration="true" timeout="4"
       ticketCompatibilityMode="Framework40" />
 </authentication>