gpt4 book ai didi

asp.net-mvc - ASP MVC 3 cookie 丢失 HttpOnly 和 Secure 标志

转载 作者:行者123 更新时间:2023-12-01 22:56:02 25 4
gpt4 key购买 nike

我将 cookie 设置为我的 mvc 应用程序的一部分:

var cookie = new HttpCookie(CookieName, encryptedData)
{
Path = FormsAuthentication.FormsCookiePath,
Domain = CookieDomain,
Expires = authenticationTicket.Expiration,
HttpOnly = true,
Secure = IsSecure // true
};
response.Cookies.Add(cookie);

现在,如果我调试,我会发现它一切正常,没有问题,并且添加了它,这也很好。然而,由于某种原因,当它实际到达浏览器时,没有设置 HttpOnly 标志或安全标志。所以我有点困惑......

我尝试在 System.Web 下的 cookie web.config 条目中设置 HttpOnly 和 Secure 标志:

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

现在这是浏览器收到响应时的样子:

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Max-Age: 10000
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: content-type, x-requested-with, *
Access-Control-Allow-Origin: http://localhost:34567
X-AspNetMvc-Version: 3.0
X-AspNet-Version: 4.0.30319
Set-Cookie: myCookie=53BA8AF84835A81E014B9174329D8543FBB6029B71C463C6FC1305D9F966F28EAA058FE103325C0F10A3012480FB0EF3F6C0BAC4703A6A6B725F383ADA35A5C125A0438FC42CADCB0DAB77953C967E6660E51C4113C6545220A0C2F86230F446D159D523BBE9CA4D9419A67BC44D23B9C4D0974DF2ED66C47EA7308D8E42E1C2280EA6059A23303E3BCBDF28F6BD4A3DFA92FFAB33DDAC8EC05D99310D26FBD6310252156CD28B89386B0D483D6D2E295EF33487E64468655371CC446E0B5DDBF12B3AA8218AF1FA929A98638A1AC729BA60815B86EAD9624ED1787172B585BE4E457C3568AB6EAAF4865E8468D04336FA7340AAC1BA75162FB322D436DC9BF50466F2F0FB3464ECF41C6C1F7001639DFE2AB2AD9CBFB65A292FE5FA42783DF331AA4641432647BA9672FE6D4C15F830E4DF8B38605852BCB15E5B01B862D966E2FD1D620730312982DB8AB4CE5EE0D0E40E6C3F5234DE5EBFA594036D912F07C3798ED429A2552AD6C4B9EC10B90749850CBDEC97F0BF7E2E43CB3991608C5D533B6EA9F8D0A7AD949B42CD3BAA13DEE99C330121B3D868B412A3435FA01C7F223641CFE441A2E07F5DFB8B23F053CBA13F5E1262A07FBFD4EC4BADF9BD5898; expires=Wed, 27-Feb-2013 19:15:24 GMT; path=/
Date: Wed, 27 Feb 2013 18:45:24 GMT
Content-Length: 2

那么我在这里错过了什么吗?或者有什么东西我没有设置在我应该设置的地方?我还使用 CORS,因为此 cookie 是从网络服务器作为身份验证机制发出的。 SSL 已启用,并且还通过 https 用于调用。即使我关闭安全cookie并使用http,HTTPOnly标志也没有被设置,所以我很困惑。

===更新===

经过仔细检查,我似乎误导了您,但是,在您第一次收到 cookie 时,HttpOnly 响应是从服务器正确发送的!当 ajax 调用将 cookie 发送到服务器时,它似乎没有添加 httponly 标志,这意味着被丢弃的 cookie 不再那么安全。 cookie 的安全部分不会在第一次响应时发送,但至少这会为其添加更多上下文。

最佳答案

试试这个,看起来是类似的问题。 ( How can I set the Secure flag on an ASP.NET Session Cookie? )

In the <system.web> element, add the following element:

<httpCookies requireSSL="true" />

However, if you have a <forms> element in your system.web\authentication block, then this will override the setting in httpCookies, setting it back to the default false.

In that case, you need to add the requireSSL="true" attribute to the forms element as well.

So you will end up with:

<system.web>
<authentication mode="Forms">
<forms requireSSL="true">
/* forms content */
</forms>
</authentication>
</system.web>

关于asp.net-mvc - ASP MVC 3 cookie 丢失 HttpOnly 和 Secure 标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15120040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com