Objective:
Dynamically pass cors origins to the cors policy from named values.
Example:
Suppose I have the following named values:
name: cors-origins
value: https://domain1.com;https://domain2.com
and the CORS policy which intends to make use of this named value cors-origins
:
<policies>
<inbound>
<cors>
<allowed-origins>
// Here I need to use {{ cors-origins }}
// The expected output is:
// <origin>https://domain1.com</origin>
// <origin>https://domain2.com</origin>
</allowed-origins>
<allowed-methods>
<method>GET</method>
<method>POST</method>
</allowed-methods>
</cors>
</inbound>
<backend>
<forward-request />
</backend>
<outbound />
<on-error />
</policies>
What I tried so far:
- Dynamically creating the
<allowed-origins>
section:
Snippet:
...
<cors>
@{
var origins = "{{cors-origins}}".Split(';');
return new XElement("allowed-origins", origins.Select(domain => new XElement("origin",domain)));
// outputs:
// <allowed-origins>
// <origin>https://domain1.com</origin>
// <origin>https://domain2.com</origin>
// </allowed-origins>
}
...
</cors>
Errors with: The element 'cors' cannot contain text. List of possible elements expected: 'allowed-origins, allowed-headers, expose-headers, allowed-methods'
- Dynamically creating only the
<origin>
elements.