如何保护ASP.NET_SessionId cookie?

Pet*_*ete 58 c# asp.net session-cookies

我已将.ASPXAUTH cookie设置为仅https,但我不确定如何使用ASP.NET_SessionId有效地执行相同操作.

整个站点使用HTTPS,因此cookie不需要同时使用http和https.

小智 141

要将; secure后缀添加到Set-Cookiehttp标头,我只需使用<httpCookies>web.config中的元素:

<system.web>
  <httpCookies httpOnlyCookies="true" requireSSL="true" />
</system.web>
Run Code Online (Sandbox Code Playgroud)

恕我直言,比编写Anubhav Goyal文章中的代码更方便.

请参阅:http://msdn.microsoft.com/en-us/library/ms228262(v = vs.100).aspx

  • Marcel Hoyer,我尝试了你的方法,但不知怎的,它只是不起作用.`asp.net_sessionid`仍然不在'secure`中.您的方法是否适用于MVC Web应用程序? (7认同)
  • 对于那些正确配置web.config且ASP.NET_SessionId仍未标记为Secure的用户,请确保在再次测试之前清除该站点的cookie.仅仅因为您已退出/未经过身份验证并不意味着您将获得新的会话cookie. (5认同)
  • http://msdn.microsoft.com/en-us/library/ms228262(v=vs.100).aspx(此MSDN主题目前不适用于.NET 4.5.) (2认同)
  • 我知道这是旧的,但我必须在MVC4项目中实现它,并且它工作得很好.添加安全; 标记到cookie. (2认同)

Joe*_*ton 43

以下是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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

将此添加到global.asax中的EndRequest事件处理程序应该可以实现所有页面调用.

注意:建议编辑break;在成功的"安全"分配中添加语句.我已经拒绝了这个编辑,因为它只允许1个cookie被强制保护而第二个被忽略.添加计数器或其他度量标准以确定两者都已被保护并在此时断开是不可想象的.但是,使用更新的代码技术,这可能会更好地编写为:

// 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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 完美,谢谢.对于读过这个可能认为它看起来像一个狡猾的解决方案的人来说,正如我第一次看到它时所做的那样,我没有发现任何暗示有更好的选择,这似乎运作良好! (4认同)
  • 会话状态配置文档的更新链接在这里:(奇怪的是 3.0 版本没有存档,但大多数其他版本都存档了)https://msdn.microsoft.com/en-us/library/h6bb9cz9(v=vs.85) .aspx。最重要的是,正如 [Marcel Hoyer 在他的回答中指出的](/sf/answers/627821631/) 你应该能够在 `httpCookies` 元素中设置 `requireSSL="true"` 和这个会起作用 - 您可能需要先清除现有的 cookie,然后才能看到更改。 (2认同)
  • 绕过循环转而使用索引运算符可能很诱人。但是,在“Response.Cookies”上调用索引运算符实际上会创建一个 cookie(如果该 cookie 尚不存在)。请注意,根据我的帖子编辑,不可能使用安全导航运算符作为分配目标。 (2认同)

Rag*_*u A 14

使用上面的Marcel解决方案来保护Forms Authentication cookie,您还应该更新"authentication"配置元素以使用SSL

<authentication mode="Forms">
   <forms ...  requireSSL="true" />
</authentication>
Run Code Online (Sandbox Code Playgroud)

其他明智的身份验证cookie将不是https

请参阅:http://msdn.microsoft.com/en-us/library/vstudio/1d3t3c61(v=vs.100).aspx

  • 这保护了表单身份验证。只吃饼干吧?不应该保护会话 cookie,这是这里的实际问题。 (2认同)

Dou*_*eny 8

发现在Session_Start中设置安全属性就足够了,正如MSDN博客" 保护会话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 = "/";
        }
    }
Run Code Online (Sandbox Code Playgroud)