2011-06-16 93 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的,你可以簡單地刪除這個檢查)。

相關問題