簡體   English   中英

如何使Azure API管理(API網關)CORS策略動態化?

[英]How to make Azure API Management (API Gateway) CORS policy dynamic?

我想在Azure API網關中動態地在allowed-origins部分添加域(起源)。 可能嗎 ? 或者我們可以設置CORS的另一種方式,以便我們可以動態地允許原點。

    <cors>
        <allowed-origins>
            <origin>http://www.example.com</origin>
        </allowed-origins>
        <allowed-methods>
            <method>GET</method>
            <method>POST</method>
        </allowed-methods>
    </cors>

您無法動態添加元素到策略配置,但您可以為它們動態提供值( https://docs.microsoft.com/en-us/azure/api-management/api-management-policy-expressions ):

<cors>
    <allowed-origins>
        <origin>@(context.Request.Url.ToUri().GetLeftPart(UriPartial.Authority))</origin>
    </allowed-origins>
    <allowed-methods>
        <method>GET</method>
        <method>POST</method>
    </allowed-methods>
</cors>

我發現了一種我提到的方式。

<inbound>
    <base />
    <send-request mode="new" response-variable-name="variable" timeout="600" ignore-error="true">
        <set-url>@("http://MyDomain/ApiMethod?Origin="(string)context.Request.Headers["Origin"].Last())</set-url>
        <set-method>GET</set-method>
        <set-body />
    </send-request>
    <cors>
        <allowed-origins>
            <origin>@((string)((IResponse)context.Variables["variable"]).Body.As<JObject>(true)["Origin"])</origin>
        </allowed-origins>
        <allowed-methods>
            <method>GET</method>
            <method>POST</method>
        </allowed-methods>
    </cors>
</inbound
  • 首先調用一個API,它將Origin作為查詢參數。
  • 將響應存儲在變量參數中。
  • API返回Json對象例如:{“Origin”:“ http://www.example.com ”}
  • 形成響應我檢索原始值。 並將其分配給<origin></origin>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM