根据上下文使用不安全 cookie 和安全 cookie

发布于 2025-01-11 02:46:32 字数 2033 浏览 5 评论 0原文

我们目前正在尝试转换一些遗留应用程序。在开发过程中,我们在安全 cookie 方面遇到了很多问题。虽然这肯定在我们的待办事项清单上,但它却停止了其他功能的工作。我们希望能够在我们的开发环境中设置 cookie,以便如果我们通过 HTTP 访问该网站,它会使用不安全的 cookie,但如果他们通过 HTTPS 访问该网站,它会使用安全 cookie。我们正在尝试使用 IIS Url Rewrite 来完成此任务。我模拟了这一点:

<rewrite>
    <outboundRules>
        <rule name="Enable secure Cookies {ProjectName}" preCondition="Missing Secure Cookies {ProjectName}">
            <match serverVariable="RESPONSE_Set_Cookie" pattern=".*" negate="false" />
            <action type="Rewrite" value="{R:0}; secure" />
            <conditions>
                <add input="{HTTPS}" pattern="^Off$" negate="true"/>
            </conditions>
        </rule>
        <rule name="Enable http only {ProjectName}" preCondition="Missing Http Only {ProjectName}">
            <match serverVariable="RESPONSE_Set_Cookie" pattern=".*" negate="false" />
            <action type="Rewrite" value="{R:0}; HttpOnly" />
            <conditions>
                <add input="{HTTPS}" pattern="^Off$" negate="true"/>
            </conditions>
        </rule>
        <preConditions>
            <preCondition name="Missing Secure Cookies {ProjectName}">
                <add input="{RESPONSE_Set_Cookie}" pattern="." />
                <add input="{RESPONSE_Set_Cookie}" pattern="; secure" negate="true" />
            </preCondition>
            <preCondition name="Missing Http Only {ProjectName}">
                <add input="{RESPONSE_Set_Cookie}" pattern="." />
                <add input="{RESPONSE_Set_Cookie}" pattern="; HttpOnly" negate="true" />
            </preCondition>
        </preConditions>
    </outboundRules>
</rewrite>

我将其添加到中。每个项目的每个 web.config 文件的标记(实际上我将 {ProjectName} 替换为项目名称,以便不同的项目具有唯一命名的规则和先决条件)。不幸的是,当我从 HTTP 访问该网站时,我仍然获得安全的 cookie。来自 HTTP 时如何否定该规则?

或者,如果 HTTPS 关闭,是否可以删除安全性和 httponly?

We are currently trying to convert some legacy applications. While in development, we are having a lot of issues with secure cookies. While that is definitely on our to-do list, it is halting work on other functionality. We would like to be able to set cookies in our dev environment so that if we reach the site via HTTP, it uses unsecure cookies, but if they reach the site via HTTPS, it uses secure cookies. We are trying to use IIS Url Rewrite to accomplish this. I mocked this up:

<rewrite>
    <outboundRules>
        <rule name="Enable secure Cookies {ProjectName}" preCondition="Missing Secure Cookies {ProjectName}">
            <match serverVariable="RESPONSE_Set_Cookie" pattern=".*" negate="false" />
            <action type="Rewrite" value="{R:0}; secure" />
            <conditions>
                <add input="{HTTPS}" pattern="^Off
quot; negate="true"/>
            </conditions>
        </rule>
        <rule name="Enable http only {ProjectName}" preCondition="Missing Http Only {ProjectName}">
            <match serverVariable="RESPONSE_Set_Cookie" pattern=".*" negate="false" />
            <action type="Rewrite" value="{R:0}; HttpOnly" />
            <conditions>
                <add input="{HTTPS}" pattern="^Off
quot; negate="true"/>
            </conditions>
        </rule>
        <preConditions>
            <preCondition name="Missing Secure Cookies {ProjectName}">
                <add input="{RESPONSE_Set_Cookie}" pattern="." />
                <add input="{RESPONSE_Set_Cookie}" pattern="; secure" negate="true" />
            </preCondition>
            <preCondition name="Missing Http Only {ProjectName}">
                <add input="{RESPONSE_Set_Cookie}" pattern="." />
                <add input="{RESPONSE_Set_Cookie}" pattern="; HttpOnly" negate="true" />
            </preCondition>
        </preConditions>
    </outboundRules>
</rewrite>

I added that to the <system.webServer> tag of every web.config file for every project (I actually replace {ProjectName} with the project's name so that different projects have uniquely named rules and preconditions). Unfortunately, I am still getting secure cookies when hitting the site from HTTP. How do I negate the rule when coming from HTTP?

Alternately, is it possible to remove security and httponly if HTTPS is off?

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

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

发布评论

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

评论(1

不…忘初心 2025-01-18 02:46:32

我的重写语法是正确的。问题是我错过了一个客户端 cookie,该 cookie 是从不安全的上下文中安全创建的,并且导致了失败。我将保留这个问题,以防其他人对同一站点中的安全和不安全 cookie 有类似的需求。对于产品来说非常糟糕的做法,但是对于开发来说非常好!

My rewrite syntax was correct. The issue was that I missed a client-side cookie that was being created as secure from an unsecure context and that was causing the failures. I will leave this question available in case someone else has a similar need for both secure and unsecure cookies in the same site. Very bad practice for prod, but excellent for development!

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