I have an operation in Azure API management platform, which calls a server for authentication, the server responds with cookies and I have to pass those cookies to the backend to be authorized.
The problem is that there is no policy or any piece of code in Microsoft Docs which allows me to do so.
Sample policy to understand the API operation more clearly:
<policies>
<inbound>
<base />
<send-request mode="new" response-variable-name="response" timeout="10" ignore-error="false">
<set-url>https://authserver.com</set-url>
<set-method>POST</set-method>
</send-request>
<set-backend-service base-url="https://backend.com" />
Now I want a policy or a piece of code which gets cookies from the above request's response and then forwards them to the backend
<value>@(((IResponse)context.Variables["response"]).Headers.GetValueOrDefault("Set-Cookie"))</value>
</set-header>
<choose>
<when condition="@(((IResponse)context.Variables["response"]).StatusCode == 200)">
<set-header name="CSRFTOKEN" exists-action="override">
<value>@(((IResponse)context.Variables["response"]).Headers.GetValueOrDefault("CSRFTOKEN"))</value>
</set-header>
<set-header name="Cookie" exists-action="override">
<value>@{
string rawcookie = ((IResponse)context.Variables["response"]).Headers.GetValueOrDefault("Set-Cookie");
string[] cookies = rawcookie.Split(';');
string cookie = cookies.FirstOrDefault( ss => ss.Contains("HASH_JSESSIONID"));
return cookie.Split(',')[1];}</value>
</set-header>
</when>
</choose>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>