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

发布于 2024-11-15 23:01:29 字数 483 浏览 3 评论 0原文

我们在负载均衡器上使用 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)?

We use Stunnel (to strip out SSL) and HAProxy on our Load Balancer - which then sends on requests to IIS via HTTP.

The problem we have is we want our sites (ASP.NET) to set cookies in a secure fashion - i.e. by setting the requireSSL attribute to true.

When we set this attribute and make a HTTPS request to the site, we get this error:

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.

Is it possible to trust the webserver if the the request is coming over SSL from the load balancer? Or is this a non-problem as it is ONLY possible to access our site via SSL (only 443 is open)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

面如桃花 2024-11-22 23:01:30

对于 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 进行的,则可以简单地删除此检查)。

For ASP.NET MVC 3 users - which we are, you can also handle this with the following GlobalFilter that I've put together - which can then secure any cookie sent back:-

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) { }
    }
}

This will set the HTTPOnly flag on any cookie regardless, then if the request is from a non-local source it will also set the secure flag. This allows for us to do local debugging over HTTP rather than HTTPS (but you can simply remove this check if everything you do is over HTTPS).

久夏青 2024-11-22 23:01:29

而不是这个:

FormsAuthentication.SetAuthCookie(email, false);

尝试这个:

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

如果您使用 ASP.NET MVC,您还可以使用全局操作过滤器,它在响应中的所有 cookie 上设置安全标志

Instead of this:

FormsAuthentication.SetAuthCookie(email, false);

try this:

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

If you are using ASP.NET MVC, you could also use a global action filter which sets the secure flag on all cookies in the response

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文