2011-06-16 90 views
2

我们在负载均衡器上使用Stunnel(剥离SSL)和HAProxy - 然后通过HTTP将请求发送到IIS。代理服务器后面的安全表单身份验证

我们的问题是我们希望我们的网站(ASP.NET)以安全的方式设置cookie - 即通过将requireSSL属性设置为true。

当我们设置这个属性,并创建一个HTTPS请求到现场,我们得到这个错误:

The application is configured to issue secure cookies. These cookies require the browser to issue the request over SSL (https protocol). However, the current request is not over SSL. 

是否有可能,如果该请求过来SSL从负载平衡器信任的Web服务器?或者这是一个非问题,因为它只能通过SSL访问我们的网站(只有443是开放的)?

回答

2

取而代之的是:

FormsAuthentication.SetAuthCookie(email, false); 

试试这个:

var cookie = FormsAuthentication.GetAuthCookie(email, false); 
cookie.Secure = true; 
HttpContext.Current.Response.Cookies.Add(cookie); 

如果您正在使用ASP.NET MVC,你也可以使用一个全球行动的过滤器设置安全标志上的所有Cookie在响应中

+0

太棒了,谢谢你让我走向正确的方向 – isNaN1247 2011-06-17 09:05:41

1

对于ASP.NET MVC 3用户 - 我们可以用下面的GlobalFilter来处理这个问题 - 我已经放在一起了 - 然后可以保护发回的任何cookie: -

namespace MyNamespace 
{ 
    public class SecureCookiesAttribute : FilterAttribute, IResultFilter 
    { 
     public void OnResultExecuting(ResultExecutingContext filterContext) 
     { 
      foreach (string cookieName in filterContext.HttpContext.Response.Cookies.AllKeys) 
       filterContext.HttpContext.Response.Cookies[cookieName].HttpOnly = true; 

      if (filterContext.HttpContext.Request.IsLocal) 
       return; 

      foreach (string cookieName in filterContext.HttpContext.Response.Cookies.AllKeys) 
       filterContext.HttpContext.Response.Cookies[cookieName].Secure = true; 
     } 

     public void OnResultExecuted(ResultExecutedContext filterContext) { } 
    } 
} 

这将在任何cookie上设置HTTPOnly标志,无论如何,那么如果请求来自非本地源,它也将设置安全标志。这允许我们通过HTTP而不是HTTPS进行本地调试(但是如果你所做的每件事都是通过HTTPS的,你可以简单地删除这个检查)。