Azure API管理案例 - 评估不如预期的表达

发布于 2025-02-12 00:24:53 字数 1845 浏览 1 评论 0原文

当我期望 true 我期望false> false

我在这里看到的内容 时格式化的消息能够比较

403: invalid client certificate 
`A5958B8D39A945764110E210A19952FA5C9CAC9A` != 
`‎A5958B8D39A945764110E210A19952FA5C9CAC9A`

负责此验证的2个指标的策略部分,如下所示:

    <inbound>
        <base />

        <set-variable name="clientCert" value="{{Client-Thumbprint}}" />
        <set-variable name="testCert" value="{{Test-Thumbprint}}" />

        <choose>
            <when condition="@(context.Request.Certificate == null || !context.Request.Certificate.Verify())">
                <return-response>
                    <set-status code="403" reason="certificate is missing or not valid" />
                </return-response>
            </when>
            <when condition='@(
                    (string)context.Request.Certificate.Thumbprint.ToUpper() != ((string)context.Variables["clientCert"]).ToUpper() && 
                    (string)context.Request.Certificate.Thumbprint.ToUpper() != ((string)context.Variables["testCert"]).ToUpper()
                )'>
                <return-response>
                    <set-status code="403" reason='@( string.Format("invalid client certificate `{0}` != `{1}`",
                            (string)context.Request.Certificate.Thumbprint.ToUpper(),
                            ((string)context.Variables["clientCert"]).ToUpper() 
                        )' />
                </return-response>
            </when>
        </choose>
        ...
    </inbound>

我敢肯定,请求的拇指打印等于clientcert变量。

我不是在问如何恢复此代码。

我的问题:当第二表达式时,第二的问题是怎么回事 - 为什么在应为false时将其评估为true

can't understand the reason why expression for the second when is evaluated to true when i'm expecting false

here what I see in response (formatted message to be able to compare 2 thumbprints)

403: invalid client certificate 
`A5958B8D39A945764110E210A19952FA5C9CAC9A` != 
`‎A5958B8D39A945764110E210A19952FA5C9CAC9A`

policy part that is responsible for this validation looks like this:

    <inbound>
        <base />

        <set-variable name="clientCert" value="{{Client-Thumbprint}}" />
        <set-variable name="testCert" value="{{Test-Thumbprint}}" />

        <choose>
            <when condition="@(context.Request.Certificate == null || !context.Request.Certificate.Verify())">
                <return-response>
                    <set-status code="403" reason="certificate is missing or not valid" />
                </return-response>
            </when>
            <when condition='@(
                    (string)context.Request.Certificate.Thumbprint.ToUpper() != ((string)context.Variables["clientCert"]).ToUpper() && 
                    (string)context.Request.Certificate.Thumbprint.ToUpper() != ((string)context.Variables["testCert"]).ToUpper()
                )'>
                <return-response>
                    <set-status code="403" reason='@( string.Format("invalid client certificate `{0}` != `{1}`",
                            (string)context.Request.Certificate.Thumbprint.ToUpper(),
                            ((string)context.Variables["clientCert"]).ToUpper() 
                        )' />
                </return-response>
            </when>
        </choose>
        ...
    </inbound>

i'm sure that thumbprint in request equal to value of clientCert variable.

i'm not asking how to rework this code.

my question: what's wrong with second when expression - why it's evaluated to true when it should be false?

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

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

发布评论

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

评论(1

浮光之海 2025-02-19 00:24:53

您可以遵循的解决方法之一,以达到上述要求;

我的问题:表达时第二次出了什么问题 - 为什么
当应该假时评估为true吗?

您制定的策略似乎是正确的,因为第二个值必须为false,我们需要使用否则之前/code>。

基于 Microsoft文档 : -

控制流策略必须至少包含一个元素。
元素是可选的。元素中的条件为
按照其在政策中的外观顺序进行评估。政策
语句(s)包含在第一个元素中的条件
属性
将应用等于true。封闭政策
如果存在,则在元素中,如果所有
元素条件属性是false

有关更多信息,请参阅此 blog | Azure:API管理中的策略

One of the workaround you can follow to achieve the above requirement ;

my question: what's wrong with second when expression - why it's
evaluated to true when it should be false?

The policy which you have made seems like correct , as the second value must be false,To do that we need to use otherwise and before when.

Based on the MICROSOFT DOCUMENTATION:-

The control flow policy must contain at least one element. The
element is optional. Conditions in elements are
evaluated in order of their appearance within the policy. Policy
statement(s) enclosed within the first element with condition
attribute
which is equals to true will be applied. Policies enclosed
within the element, if present, will be applied if all of
the element condition attributes are false
.

For more information Please refer this Blog|Azure: Policies in API Management .

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