如何在 ASP.NET 会话 Cookie 上设置安全标志?
如何在 ASP.NET 会话 Cookie 上设置安全标志,以便它仅通过 HTTPS 传输,而绝不会通过纯 HTTP 传输?
How can I set the Secure flag on an ASP.NET Session Cookie, so that it will only be transmitted over HTTPS and never over plain HTTP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在
元素中,添加以下元素:但是,如果您的
system.web\authentication 中有
块,那么这将覆盖
元素httpCookies
中的设置,将其设置回默认的false
。在这种情况下,您还需要将
requireSSL="true"
属性添加到 forms 元素。因此,您最终会得到:
请参阅此处< /a> 和 这里获取这些元素的 MSDN 文档。
In the
<system.web>
element, add the following element:However, if you have a
<forms>
element in yoursystem.web\authentication
block, then this will override the setting inhttpCookies
, setting it back to the defaultfalse
.In that case, you need to add the
requireSSL="true"
attribute to the forms element as well.So you will end up with:
See here and here for MSDN documentation of these elements.
有两种方法,
web.config
中的一个httpCookies
元素允许您打开requireSSL
,它仅传输所有 cookie,包括仅使用 SSL 的会话,以及也在表单身份验证中,但如果您在 httpcookies 上打开 SSL,您也必须在表单配置中打开它。为清楚起见进行编辑:
将其放入
There are two ways, one
httpCookies
element inweb.config
allows you to turn onrequireSSL
which only transmit all cookies including session in SSL only and also inside forms authentication, but if you turn on SSL on httpcookies you must also turn it on inside forms configuration too.Edit for clarity:
Put this in
<system.web>
如果您谈论的是企业环境中签入的代码,事情很快就会变得混乱。我们发现最好的方法是让 web.Release.config 包含以下内容:
这样,开发人员就不会受到影响(在调试模式下运行),并且只有获得发布版本的服务器才需要cookie 为 SSL。
Things get messy quickly if you are talking about checked-in code in an enterprise environment. We've found that the best approach is to have the web.Release.config contain the following:
That way, developers are not affected (running in Debug), and only servers that get Release builds are requiring cookies to be SSL.
基于@Mark D 的回答,我将使用 web.config 转换将所有各种 cookie 设置为安全。这包括设置
anonymousIdentification cookieRequireSSL
和httpCookies requireSSL
。为此,您将 web.Release.config 设置为:
如果您将角色和表单身份验证与
ASP.NET Membership Provider
(我知道,它很古老)一起使用,您还需要将roleManager cookieRequireSSL
和forms requireSSL
属性也设置为安全。如果是这样,您的 web.release.config 可能如下所示(包含在上面以及会员 API 的新标签):web.config 的背景在此处转换: http://go.microsoft.com/fwlink/?LinkId=125889
显然这超出了OP的原始问题,但如果你不将它们全部设置为安全 您可以预期安全扫描工具会注意到,并且您会在报告中看到危险信号。问我怎么知道的。 :)
Building upon @Mark D's answer I would use web.config transforms to set all the various cookies to Secure. This includes setting
anonymousIdentification cookieRequireSSL
andhttpCookies requireSSL
.To that end you'd setup your web.Release.config as:
If you're using Roles and Forms Authentication with the
ASP.NET Membership Provider
(I know, it's ancient) you'll also want to set theroleManager cookieRequireSSL
and theforms requireSSL
attributes as secure too. If so, your web.release.config might look like this (included above plus new tags for membership API):Background on web.config transforms here: http://go.microsoft.com/fwlink/?LinkId=125889
Obviously this goes beyond the original question of the OP but if you don't set them all to secure you can expect that a security scanning tool will notice and you'll see red flags appear on the report. Ask me how I know. :)
secure - 此属性告诉浏览器仅在通过安全通道(例如 HTTPS)发送请求时才发送 cookie。这将有助于防止 cookie 通过未加密的请求传递。如果应用程序可以通过 HTTP 和 HTTPS 访问,那么 cookie 就有可能以明文形式发送。
secure - This attribute tells the browser to only send the cookie if the request is being sent over a secure channel such as HTTPS. This will help protect the cookie from being passed over unencrypted requests. If the application can be accessed over both HTTP and HTTPS, then there is the potential that the cookie can be sent in clear text.