{"id":2451,"date":"2019-06-26T09:47:55","date_gmt":"2019-06-26T07:47:55","guid":{"rendered":"https:\/\/laurentvanacker.com\/?p=2451"},"modified":"2022-05-30T13:52:47","modified_gmt":"2022-05-30T11:52:47","slug":"session-state-and-session-cookies-best-practices","status":"publish","type":"post","link":"https:\/\/laurentvanacker.com\/index.php\/2019\/06\/26\/session-state-and-session-cookies-best-practices\/","title":{"rendered":"Session state and session cookies best practices"},"content":{"rendered":"<p><strong>Best practices for the session state:<\/strong><\/p>\n<ul>\n<li>Change the default session ID name. In ASP.NET, the default name is ASP.NET_SessionId. This immediately gives away that the application is ASP.NET and that that cookie contains the session ID value<\/li>\n<li>Make sure the length of the session ID is long enough to prevent brute force attacks. Recommended length is 128 bits<\/li>\n<li>Make sure to create the session ID in a completely random way. This ensures that attackers can\u2019t guess the session ID by using predictability analysis<\/li>\n<li>Ensure that the session ID does not contain any additional sensitive data. The data should be a random string of characters with no meaning<\/li>\n<li>HTTPS should be used for all session based applications handling sensitive data<\/li>\n<li>Session cookies should be created with the Secure and HttpOnly attributes<\/li>\n<li>Prevent concurrent sessions where possible<\/li>\n<li>Destroy sessions upon timeout, logoff, browser close or log-in from a separate location<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Best practices for the session cookies:<\/strong><\/p>\n<ul>\n<li>Do not store any critical information in cookies. For example, do not store a user\u2019s password in a cookie. As a rule, do not keep anything in a cookie that can compromise your application. Instead, keep a reference in the cookie to a location on the server where the data is<\/li>\n<li>Set expiration dates on cookies to the shortest practical time. Avoid using permanent cookies<\/li>\n<li>Consider encrypting information in cookies<\/li>\n<li>Consider setting the Secure and HttpOnly properties on the cookie to true<\/li>\n<\/ul>\n<p><strong>Example<\/strong><br \/>\nHere are a few examples of implementing best practices for cookies:<\/p>\n<p>Web.config file:<\/p>\n<pre class=\"brush: xml; collapse: true; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n&lt;system.web&gt;\r\n    &lt;sessionState regenerateExpiredSessionId=&quot;false&quot; cookieless=&quot;UseCookies&quot; cookieName=&quot;id&quot; \/&gt;\r\n&lt;\/system.web&gt;\r\n\r\n<\/pre>\n<p>Code-behind file:<\/p>\n<pre class=\"brush: csharp; collapse: true; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\nResponse.Cookies.Add(new HttpCookie(&quot;id&quot;, &quot;&quot;));\r\nResponse.Cookies&#x5B;&quot;id&quot;].HttpOnly = true;\r\nResponse.Cookies&#x5B;&quot;id&quot;].Secure = Convert.ToBoolean(ConfigurationManager.AppSettings&#x5B;&quot;SecureCookie&quot;]);\r\n\r\n<\/pre>\n<p><strong>References:<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/docs.microsoft.com\/en-us\/previous-versions\/ms178194(v=vs.140)?redirectedfrom=MSDN\">ASP.NET Cookies Overview<\/a><\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/en-us\/previous-versions\/zdh19h94(v=vs.140)?redirectedfrom=MSDN#cpconbestsecuritypracticesforwebapplicationsanchor8\">Use Cookies Securely<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Laurent<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Best practices for the session state: Change the default session ID name. In ASP.NET, the default name is ASP.NET_SessionId. This immediately gives away that the [&#8230;]<\/p>\n","protected":false},"author":2,"featured_media":2461,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,7,12],"tags":[20,25,42],"class_list":["post-2451","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net","category-iis","category-securite-security","tag-net","tag-iis","tag-security"],"_links":{"self":[{"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/posts\/2451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/comments?post=2451"}],"version-history":[{"count":13,"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/posts\/2451\/revisions"}],"predecessor-version":[{"id":2816,"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/posts\/2451\/revisions\/2816"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/media\/2461"}],"wp:attachment":[{"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/media?parent=2451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/categories?post=2451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/tags?post=2451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}