我有一个.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} }
我将cookie设置错了吗?
答案 0 :(得分:1)
在system.web下的web.config中添加以下配置
<httpCookies httpOnlyCookies="false" requireSSL="false" />