如何在ASP.NET会话Cookie上设置安全标志?

Ale*_*lex 130 ssl-security asp.net-session

如何在ASP.NET会话Cookie上设置安全标志,以便它只通过HTTPS传输,而不是通过纯HTTP传输?

Mar*_*den 169

<system.web>元素中,添加以下元素:

<httpCookies requireSSL="true" />
Run Code Online (Sandbox Code Playgroud)

但是,如果块中有<forms>元素,system.web\authentication则会覆盖该设置httpCookies,将其设置回默认值false.

在这种情况下,您还需要将requireSSL="true"属性添加到forms元素.

所以你最终会得到:

<system.web>
    <authentication mode="Forms">
        <forms requireSSL="true">
            <!-- forms content -->
        </forms>
    </authentication>
</system.web>
Run Code Online (Sandbox Code Playgroud)

有关这些元素的MSDN文档,请参见此处此处.

  • 此外,如果有一个 `roleManager` 元素,它的属性 `cookieRequireSSL="true"` 也应该设置为 true。参考 https://msdn.microsoft.com/en-us/library/system.web.security.roles.cookierequiressl(v=vs.110).aspx (4认同)
  • 您可以通过包含'lockItem'属性来避免覆盖<httpCookies requireSSL ="true"/>设置的其他web.config设置.像这样:<httpCookies requireSSL ="true"lockItem ="true"/>.更多信息,请访问http://www.dotnetnoob.com/2010/11/how-to-secure-aspnet-cookies.html (2认同)

Aka*_*ava 111

有两种方法,一个httpCookies元素web.config允许你打开requireSSL,它只传输所有的cookie,包括仅在SSL中的会话以及表单内部认证,但是如果你在httpcookies上打开SSL,你也必须在表单配置中打开它.

为清晰起见编辑: 放入<system.web>

<httpCookies requireSSL="true" />
Run Code Online (Sandbox Code Playgroud)

  • +1为了澄清,这是您应该添加到web.config以将auth cookie上的安全标志设置为true` <httpCookies requireSSL ="true"/>` (12认同)
  • 请注意,这取决于您的(服务器级别)配置.我带来了测试区域,错误"应用程序配置为发出安全cookie.这些cookie要求浏览器通过SSL发出请求(https协议).但是,当前请求不是通过SSL." 这是因为我们有一个反向代理,浏览器通过SSL连接到它,但IIS服务器的反向代理通过端口80,因此应用程序不认为它是安全的. (7认同)
  • @Bargitta我们处理了Application_PreSendRequestHeaders事件,如果某个应用程序设置为true,我们将_all_ cookies设置为secure.此应用设置仅适用于我们的HTTPS外部网站. (4认同)

Mar*_*k D 19

如果您在企业环境中讨论签入代码,事情会很快变得混乱.我们发现最好的方法是让web.Release.config包含以下内容:

<system.web>
  <compilation xdt:Transform="RemoveAttributes(debug)" />
  <authentication>
      <forms xdt:Transform="Replace" timeout="20" requireSSL="true" />
  </authentication>
</system.web>
Run Code Online (Sandbox Code Playgroud)

这样,开发人员不会受到影响(在Debug中运行),只有获得Release版本的服务器才需要cookie为SSL.

  • ^^^就是这样^^^。有关 Web.Config 转换的更多信息:http://go.microsoft.com/fwlink/?LinkId=125889 (2认同)

Jef*_*ler 6

基于@Mark D 的回答,我将使用 web.config 转换将所有各种 cookie 设置为安全。这包括设置anonymousIdentification cookieRequireSSLhttpCookies requireSSL

为此,您需要将 web.Release.config 设置为:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.web>
    <httpCookies xdt:Transform="SetAttributes(httpOnlyCookies)" httpOnlyCookies="true" />
    <httpCookies xdt:Transform="SetAttributes(requireSSL)" requireSSL="true" />
    <anonymousIdentification xdt:Transform="SetAttributes(cookieRequireSSL)" cookieRequireSSL="true" /> 
  </system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)

如果您将角色和表单身份验证与ASP.NET Membership Provider(我知道,它很古老)一起使用,您还需要将roleManager cookieRequireSSLforms requireSSL属性设置为安全。如果是这样,您的 web.release.config 可能如下所示(包含在上面以及会员 API 的新标签):

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.web>
    <httpCookies xdt:Transform="SetAttributes(httpOnlyCookies)" httpOnlyCookies="true" />
    <httpCookies xdt:Transform="SetAttributes(requireSSL)" requireSSL="true" />
    <anonymousIdentification xdt:Transform="SetAttributes(cookieRequireSSL)" cookieRequireSSL="true" /> 
    <roleManager xdt:Transform="SetAttributes(cookieRequireSSL)" cookieRequireSSL="true" />
    <authentication>
        <forms xdt:Transform="SetAttributes(requireSSL)" requireSSL="true" />
    </authentication>
  </system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)

web.config 的背景在此转换:http://go.microsoft.com/fwlink/? LinkId=125889

显然,这超出了OP的原始问题,但如果您没有将它们全部设置为安全,您可以预期安全扫描工具会注意到,并且您会在报告上看到危险信号。问我怎么知道的。:)