【发布时间】:2011-08-24 03:13:30
【问题描述】:
我已将 .ASPXAUTH cookie 设置为仅 https,但我不确定如何有效地对 ASP.NET_SessionId 执行相同操作。
整个站点使用 HTTPS,因此 cookie 无需同时使用 http 和 https。
【问题讨论】:
标签: c# asp.net session-cookies
我已将 .ASPXAUTH cookie 设置为仅 https,但我不确定如何有效地对 ASP.NET_SessionId 执行相同操作。
整个站点使用 HTTPS,因此 cookie 无需同时使用 http 和 https。
【问题讨论】:
标签: c# asp.net session-cookies
也值得考虑:
__Secure-, which signals to the browser that the Secure attribute is required.
__Host-, which signals to the browser that both the Path=/ and Secure attributes are required, and at the same time, that the Domain attribute must not be present.
一篇关于为什么会有帮助的好文章
https://check-your-website.server-daten.de/prefix-cookies.html
而不是使用清楚识别编程语言的名称。
例如
ASP.NET_SessionId = __Secure-SID
sameSite="Lax"
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite
requireSSL="true"
<sessionState cookieless="false" cookieName="__Secure-SID" cookieSameSite="Lax" />
<httpCookies httpOnlyCookies="true" sameSite="Lax" requireSSL="true" />
【讨论】:
这是取自blog article written by Anubhav Goyal的代码sn-p:
// this code will mark the forms authentication cookie and the
// session cookie as Secure.
if (Response.Cookies.Count > 0)
{
foreach (string s in Response.Cookies.AllKeys)
{
if (s == FormsAuthentication.FormsCookieName || "asp.net_sessionid".Equals(s, StringComparison.InvariantCultureIgnoreCase))
{
Response.Cookies[s].Secure = true;
}
}
}
将它添加到 global.asax 中的 EndRequest 事件处理程序应该会在所有页面调用中发生这种情况。
注意:建议进行编辑以在成功的“安全”分配中添加 break; 语句。我拒绝了这个编辑,因为它只允许强制保护 1 个 cookie,而第二个将被忽略。添加计数器或其他一些指标来确定两者都已得到保护并在此时中断并不是不可想象的。
【讨论】:
ASP.NET_SessionId。可以覆盖msdn.microsoft.com/en-us/library/h6bb9cz9.aspx
httpCookies 元素中设置requireSSL="true",这将起作用-您可能需要先清除现有cookie,然后才能看到更改。
Response.Cookies 上调用索引运算符实际上会创建一个 cookie,如果该 cookie 尚不存在的话。请注意,根据我的帖子编辑,不可能将安全导航运算符用作分配目标。
要将; secure 后缀添加到Set-Cookie http 标头,我只是在web.config 中使用了<httpCookies> 元素:
<system.web>
<httpCookies httpOnlyCookies="true" requireSSL="true" />
</system.web>
恕我直言,比在 Anubhav Goyal 的文章中编写代码更方便。
见:http://msdn.microsoft.com/en-us/library/ms228262(v=vs.100).aspx
【讨论】:
asp.net_sessionid 仍然不在 secure 中。您的方法是否适用于 MVC Web 应用程序?
添加到@JoelEtherton 的解决方案以修复新发现的安全漏洞。如果用户请求 HTTP 并被重定向到 HTTPS,则会发生此漏洞,但 sessionid cookie 在第一次请求 HTTP 时被设置为安全。据 McAfee Secure 称,这现在是一个安全漏洞。
仅当请求使用 HTTPS 时,此代码才会保护 cookie。如果不是 HTTPS,它将使 sessionid cookie 过期。
// this code will mark the forms authentication cookie and the
// session cookie as Secure.
if (Request.IsSecureConnection)
{
if (Response.Cookies.Count > 0)
{
foreach (string s in Response.Cookies.AllKeys)
{
if (s == FormsAuthentication.FormsCookieName || s.ToLower() == "asp.net_sessionid")
{
Response.Cookies[s].Secure = true;
}
}
}
}
else
{
//if not secure, then don't set session cookie
Response.Cookies["asp.net_sessionid"].Value = string.Empty;
Response.Cookies["asp.net_sessionid"].Expires = new DateTime(2018, 01, 01);
}
【讨论】:
发现在 Session_Start 中设置安全属性就足够了,正如 MSDN 博客“Securing Session ID: ASP/ASP.NET”中所建议的那样,并进行了一些扩充。
protected void Session_Start(Object sender, EventArgs e)
{
SessionStateSection sessionState =
(SessionStateSection)ConfigurationManager.GetSection("system.web/sessionState");
string sidCookieName = sessionState.CookieName;
if (Request.Cookies[sidCookieName] != null)
{
HttpCookie sidCookie = Response.Cookies[sidCookieName];
sidCookie.Value = Session.SessionID;
sidCookie.HttpOnly = true;
sidCookie.Secure = true;
sidCookie.Path = "/";
}
}
【讨论】:
使用上述 Marcel 的解决方案来保护表单身份验证 cookie,您还应该更新“身份验证”配置元素以使用 SSL
<authentication mode="Forms">
<forms ... requireSSL="true" />
</authentication>
其他明智的身份验证cookie不会是https
见:http://msdn.microsoft.com/en-us/library/vstudio/1d3t3c61(v=vs.100).aspx
【讨论】:
如果整个站点都使用 HTTPS,那么您的 sessionId cookie 至少与 HTTPS 加密一样安全。这是因为 cookie 是作为 HTTP 标头发送的,而当使用 SSL 时,HTTP 标头在传输时会使用 SSL 进行加密。
【讨论】: