将 cookie 标记为“安全”当网站位于提供 TLS 证书的负载均衡器后面时,在表单身份验证中
最近,我在 AppHarbor 上的一个网站上遇到了一些问题,我在他们的支持论坛上写过该网站: Request.IsSecureConnection 始终返回 false
简而言之,由于负载均衡器在 HTTPS 流量到达 Web 应用程序之前对其进行解密,因此诸如Request.IsSecureConnection
和表单身份验证上的 requireSSL
等配置未按预期运行。事实上,在后一种情况下,您甚至无法进行身份验证,因为应用程序认为请求不是通过 HTTPS 发送的。
表单身份验证尤其有问题,因为如果没有它,cookie 就不会设置为“安全”,并且如果仅通过域名访问站点并隐式提供不安全的 URL 方案,则 cookie 不会通过 HTTP 发送回来。
对此最好的解决方法是什么?我更喜欢利用本机安全配置,任何人都可以找到一种方法来覆盖检查连接是否安全的实现吗?很容易检测请求是否通过 HTTPS 提供(基于 Request.Url.Scheme
或 X_FORWARDED_FOR
标头),这只是一个将其整齐地绑定到的问题。
Recently I had a bit of a problem with a site on AppHarbor which I wrote about on their support forums: Request.IsSecureConnection always returns false
In short, because the load balancer is decrypting the HTTPS traffic before it hits the web app, attributes such as Request.IsSecureConnection
and configuration like requireSSL
on forms auth is not behaving as expected. In fact in the latter case, you can't even authenticate as the app thinks the request isn't coming over HTTPS.
It's the forms auth which is especially problematic because without it, cookies aren't set to "secure" and are sent back over HTTP if the site is accessed by domain name only and implicitly serves up the insecure URL scheme.
What would be the best workaround for this? I'd prefer to leverage the native security configuration, can anyone see a way to override the implementation which checks if the connection is secure? It's easy enough to detect whether the request was served over HTTPS (either based on Request.Url.Scheme
or the X_FORWARDED_FOR
header), it's just a question of neatly tying this in.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者,您可以使用 URL 重写模块来欺骗 ASP.NET,使其认为它正在 HTTPS 上下文中运行,并保留 requireSSL 标志(这意味着 cookie 将被设置为安全的 - 并且仅在您时才可用>确实在 HTTPS 下运行)
您可以在 web.config 中设置以下内容:
您还需要将 HTTPS 添加到 applicationHost.config 中的 allowedServerVariables 列表中(或通过 URL REwrite 配置)
谢谢到ASP.NET 团队的 Levi Broderick 给我指明了这个解决方案的正确方向!
Alternatively, you can use the URL rewrite module to trick ASP.NET into thinking that it is running under a HTTPS context, and leave the requireSSL flags in place (which means the cookies will be set as secure - and only available if you really are running under HTTPS)
You can set the following in your web.config:
You'll also need to add HTTPS to the list of allowedServerVariables in the applicationHost.config (or through the URL REwrite config)
With thanks to Levi Broderick on the ASP.NET team who sent me in the right direction to this solution!
关闭 RequireSSL 设置并将以下代码添加到您的应用程序中。这应该可以保护您的身份验证/会话 cookie。
Turn off the RequireSSL setting and add the following code to your app. This should secure your authentication/session cookies.