Asp.net 代理背后的安全表单身份验证

Asp.net 代理背后的安全表单身份验证,asp.net,ssl,forms-authentication,haproxy,Asp.net,Ssl,Forms Authentication,Haproxy,我们在负载平衡器上使用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 requ

我们在负载平衡器上使用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是开放的)?

而不是:

FormsAuthentication.SetAuthCookie(email, false);
试试这个:

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

如果您使用的是ASP.NET MVC,您还可以使用一个全局操作筛选器,该筛选器为ASP.NET MVC 3用户在响应中的所有cookie设置安全标志。我们是ASP.NET MVC 3用户,您也可以使用我组合的以下全局筛选器来处理此问题,然后该筛选器可以保护发送回的任何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进行的,则可以简单地删除此检查)