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)
Aka*_*ava 111
有两种方法,一个httpCookies元素web.config允许你打开requireSSL,它只传输所有的cookie,包括仅在SSL中的会话以及表单内部认证,但是如果你在httpcookies上打开SSL,你也必须在表单配置中打开它.
为清晰起见编辑:
放入<system.web>
<httpCookies requireSSL="true" />
Run Code Online (Sandbox Code Playgroud)
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.
基于@Mark D 的回答,我将使用 web.config 转换将所有各种 cookie 设置为安全。这包括设置anonymousIdentification cookieRequireSSL和 httpCookies 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 cookieRequireSSL和forms 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的原始问题,但如果您没有将它们全部设置为安全,您可以预期安全扫描工具会注意到,并且您会在报告上看到危险信号。问我怎么知道的。:)
| 归档时间: |
|
| 查看次数: |
157084 次 |
| 最近记录: |