Tro*_*unt 9 asp.net security cookies ssl load-balancing
最近我在AppHarbor上的一个网站遇到了一些问题,我在他们的支持论坛上写过:Request.IsSecureConnection总是返回false
简而言之,因为负载均衡器在它到达Web应用程序之前解密HTTPS流量,所以表单auth Request.IsSecureConnection
等属性和配置requireSSL
不符合预期.实际上在后一种情况下,您甚至无法进行身份验证,因为应用程序认为请求不是通过HTTPS传递的.
这是表单auth,这是特别有问题的,因为没有它,cookie不会设置为"安全",并且如果站点仅通过域名访问并且隐式提供不安全的URL方案,则通过HTTP发回.
对此最好的解决方法是什么?我更喜欢利用本机安全配置,任何人都可以看到一种方法来覆盖检查连接是否安全的实现?很容易检测请求是通过HTTPS提供的(基于Request.Url.Scheme
或是X_FORWARDED_FOR
标头),这只是一个整齐地将其绑定的问题.
或者,您可以使用URL重写模块来欺骗ASP.NET,使其认为它在HTTPS上下文中运行,并保留requireSSL标志(这意味着cookie将被设置为安全 - 并且只有在您确实存在时才可用)在HTTPS下运行)
您可以在web.config中设置以下内容:
<rewrite>
<rules>
<rule name="HTTPS_AlwaysOn" patternSyntax="Wildcard">
<match url="*" />
<serverVariables>
<set name="HTTPS" value="on" />
</serverVariables>
<action type="None" />
</rule>
</rules>
</rewrite>
Run Code Online (Sandbox Code Playgroud)
您还需要将HTTPS添加到applicationHost.config中的allowedServerVariables列表中(或通过URL REwrite config)
<rewrite>
<allowedServerVariables>
<add name="HTTPS" />
</allowedServerVariables>
</rewrite>
Run Code Online (Sandbox Code Playgroud)
感谢ASP.NET团队中的Levi Broderick,他向我发出了正确的解决方案!
小智 4
关闭 RequireSSL 设置并将以下代码添加到您的应用程序中。这应该可以保护您的身份验证/会话 cookie。
void Application_EndRequest(object sender, EventArgs e)
{
if (Response.Cookies.Count > 0)
{
foreach (string s in Response.Cookies.AllKeys)
{
if (s == FormsAuthentication.FormsCookieName || s.ToLower() == "asp.net_sessionid")
{
Response.Cookies[s].Secure = true;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2641 次 |
最近记录: |