[英]How to secure the ASP.NET_SessionId cookie?
我已將 .ASPXAUTH cookie 設置為僅 https,但我不確定如何有效地對 ASP.NET_SessionId 執行相同的操作。
整個站點使用 HTTPS,因此 cookie 不需要同時使用 http 和 https。
添加; secure
為Set-Cookie
http 標頭添加; secure
后綴 我只是在 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
這是從Anubhav Goyal 撰寫的博客文章中摘取的代碼片段:
// 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,而第二個將被忽略。 添加一個計數器或其他一些指標來確定兩者都已得到保護並在該點中斷,這並非不可想象。
使用上面 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
發現在 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 = "/";
}
}
添加到@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);
}
也值得考慮:
__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" />
如果整個站點都使用 HTTPS,那么您的 sessionId cookie 至少與 HTTPS 加密一樣安全。 這是因為 cookie 是作為 HTTP 標頭發送的,當使用 SSL 時,HTTP 標頭在傳輸時會使用 SSL 進行加密。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.