我添加的cookie上的HttpOnly始终设置为true

时间:2019-06-11 01:25:06

标签: c# asp.net cookies

我有一个.NET Web应用程序,其中有一些自定义代码将auth令牌存储在cookie中:

if (HttpContext.Current.Response.Cookies[AuthTokenName] == null)
{
    HttpCookie authCookie = new HttpCookie(AuthTokenName);
    authCookie.HttpOnly = false;
    authCookie.Value = result.AccessToken;
    authCookie.Expires = DateTime.Now.AddDays(-1);

    // tried both of these methods
    HttpContext.Current.Response.AppendCookie(authCookie);
    //HttpContext.Current.Response.Cookies.Add(authCookie);
}
else
{
    HttpContext.Current.Response.Cookies[AuthTokenName].Value = result.AccessToken;
}

我需要通过javascript访问客户端的cookie,因此HttpOnly必须为false。我专门将HttpOnly设置为false,但是我的JS无法访问令牌,这是因为当我查看浏览器cookie时,HttpOnly设置为{{1} }

enter image description here

我将cookie设置错了吗?

1 个答案:

答案 0 :(得分:1)

在system.web下的web.config中添加以下配置

<httpCookies httpOnlyCookies="false" requireSSL="false" />