简体   繁体   English

如何使Azure API管理(API网关)CORS策略动态化?

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

I want to add domains(Origins) in allowed-origins section dynamically in Azure API gateway. 我想在Azure API网关中动态地在allowed-origins部分添加域(起源)。 Is it possible ? 可能吗 ? Or is there another way we can setup the CORS, so that we can allow origins dynamically. 或者我们可以设置CORS的另一种方式,以便我们可以动态地允许原点。

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

You can't dynamically add elements to policy configuration, but you can dynamically provide values for them ( https://docs.microsoft.com/en-us/azure/api-management/api-management-policy-expressions ): 您无法动态添加元素到策略配置,但您可以为它们动态提供值( 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>

I found one way that I'm mentioning bellow. 我发现了一种我提到的方式。

<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
  • First called a API which takes the Origin as query parameter. 首先调用一个API,它将Origin作为查询参数。
  • stored the response in variable parameter. 将响应存储在变量参数中。
  • API Returns Json Object eg: { "Origin": " http://www.example.com " } API返回Json对象例如:{“Origin”:“ http://www.example.com ”}
  • Form the response I retrieve the Origin Value. 形成响应我检索原始值。 and assign it to <origin></origin> 并将其分配给<origin></origin>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 XML 语法帮助,同时创建 Azure API 管理策略 - XML syntax help while creating Azure API Management policy 如何将POST azure api管理系统中的输入参数转换为XML,并将其作为请求正文? - How to transform input parameters in POST azure api management system to XML and make it as request body? 如何使用 API 管理的 Set-body 策略为 XML 元素设置值 - How to set value to XML element with Set-body Policy of API Management Azure API策略查找和替换-从“ &lt;”到“ &lt;” - Azure API Policy Find And Replace - from “&lt;” to “<” Azure rest api - APIM 策略 Z3501BB093D363810B671059B9CFED 错误 - Azure rest api - APIM policy XML errors 通过JWT令牌进行Azure API管理中的授权 - Authorization in Azure API management through JWT token Azure Api 管理 Xml 架构验证 - Azure Api Management Xml schema validation 使用Azure Rest API以编程方式将管理证书添加到Azure - Adding Management Certificate programatically to Azure using Azure Rest API 从Azure服务管理API返回的XML中提取值 - Extracting values from XML returned by azure service management API 将响应转发到其他URL时,将响应主体从XML转换为API管理策略表达式中的Json - Convert response body from XML to Json in API Management Policy Expression when forwarding response to different URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM