从Javascript访问在服务器端创建的Cookie

时间:2017-01-13 06:44:35

标签: javascript c# asp.net session cookies

我创建了一个usercontrol,我在整个应用程序中使用它来在会话超时发生时自动注销用户。为此,我使用cookie变量。 我需要在发生回发后重置cookie变量,因为这意味着用户处于活动状态。以下代码位设置将保存到cookie“express”的到期时间

DateTime date = DateTime.Now;
LoginDate = date.ToString("u", DateTimeFormatInfo.InvariantInfo).Replace("Z","");
int sessionTimeout = Session.Timeout;
dateExpress = date.AddMinutes(sessionTimeout);
ExpressDate = dateExpress.ToString("u", DateTimeFormatInfo.InvariantInfo).Replace("Z", "");

HttpCookie cookie = Request.Cookies["express"];
if (cookie == null)
{
  cookie = new HttpCookie("express");
  cookie.Path = "/somepath/";
  cookie.Values["express"] = ExpressDate;
}
else
{
  cookie.Values["express"] = ExpressDate;
}

我正在使用以下javascript代码从客户端计时器中检索服务器端的cookie集,该计时器会持续检查会话是否已过期

<script type="text/javascript">
var timeRefresh;
var timeInterval;
var currentTime;
var expressTime;

expressTime = "<%=ExpressDate %>";
currentTime = "<%=LoginDate %>";
//setCookie("express", expressTime);
timeRefresh = setInterval("Refresh()", 10000);

// Refresh this page to check session is expire or timeout.
function Refresh() {

var current = getCookie("express");
alert(current);
}

// Retrieve cookie by name.
function getCookie(name) {
    var arg = name + "=";
    var aLen = arg.length;
    var cLen = document.cookie.length;
    var i = 0;
    while (i < cLen) {
        var j = i + aLen;
        if (document.cookie.substring(i, j) == arg) {
            return getCookieVal(j);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return;
}

function getCookieVal(offSet) {
    var endStr = document.cookie.indexOf(";", offSet);
    if (endStr == -1) {
        endStr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offSet, endStr));
}

但我总是从客户端获取cookie的未定义值。

PS:我也尝试使用在服务器端创建的会话变量并从javascript访问它,但是当它读取会话值时,它只读取在页面加载时分配的第一个值,我想在此处获取会话值下次回发

我在pageload()

中设置session express
 Session["express"] = ExpressDate;

然后尝试从像这样的javascript访问它

function Refresh()`
{
var current = '<%=Session["expiry"].ToString()%>'
alert(current) // This always return the first value assigned during page load
}`

非常感谢任何帮助

3 个答案:

答案 0 :(得分:0)

您有两种Cookie,仅限服务器或两者都有。

HttpCookie有一个HttpOnly属性。只需将其设置为false。

  

获取或设置一个值,该值指定客户端脚本是否可以访问cookie。

cookie.HttpOnly = false; //default value

修改 一种简单的调试方法是查看开发人员控制台内部并与您的字符串进行比较。

enter image description here

答案 1 :(得分:0)

我怀疑path可能会造成问题。当您将path设置为/somepath/时,只有具有/somepath/的请求才能在客户端访问Cookie。

例如,

您正在请求页面http://example.com/somepath/login并在服务器上设置Cookie,路径值为/somepath/login

下次,您正在重新访问页面http://example.com/otherpath/done并尝试访问客户端的Cookie。由于您在服务器的路径/somepath/login中设置了cookie,因此您不会通过此请求获取这些cookie。

因此,请确保您已为Cookie设置了正确的路径,或者未将其设置为/,并且在此路径上设置的Cookie可在同一域http://example.com下的所有请求中访问。

答案 2 :(得分:0)

找到丢失的部分。Jaganath在设置'/'

的路径时是正确的

我只需将创建的cookie添加到响应中,只有在下一个请求获得对回发之前创建的cookie的访问权之后

这就是我做的

HttpCookie cookie = HttpContext.Current.Request.Cookies["express"];

    if (cookie == null)
    {
        cookie = new HttpCookie("express");
    }
    cookie.HttpOnly = false;
    cookie.Path = "/";
    cookie.Values["express"] = ExpressDate;
    Response.Cookies.Add(cookie);

全部谢谢