【Azure APIM】APIM 策略语句如何来设置多个Cookie值让浏览器保存

问题描述

在APIM的 <return-response> 策略中,设置Cookie值,因为需要设置多个Cookie值,使用下面两种方式都只能保存一个Cookie值:

方式一:把多个cookie值用分号(;)拼接

        &lt;return-response&gt;
            &lt;set-status code="201" /&gt;
            &lt;set-header name="Set-Cookie" exists-action="override"&gt;
                &lt;value&gt;@("cookie0=000000; cookie1=" + context.Variables.GetValueOrDefault&lt;string&gt;("token", "no value"))&lt;/value&gt;
            &lt;/set-header&gt;
            &lt;set-body&gt;@(context.Variables.GetValueOrDefault&lt;string&gt;("token", "no value"))&lt;/set-body&gt;
        &lt;/return-response&gt;

方式二:使用多个 set-header name=“Set-Cookie” 节点

        &lt;return-response&gt;
            &lt;set-status code="201" /&gt;
            &lt;set-header name="Set-Cookie" exists-action="override"&gt;
                &lt;value&gt;cookie0=000000&lt;/value&gt;
            &lt;/set-header&gt;
            &lt;set-header name="Set-Cookie" &gt;
                &lt;value&gt;@("cookie1=" + context.Variables.GetValueOrDefault&lt;string&gt;("token", "no value"))&lt;/value&gt;
            &lt;/set-header&gt;
            &lt;set-body&gt;@(context.Variables.GetValueOrDefault&lt;string&gt;("token", "no value"))&lt;/set-body&gt;
        &lt;/return-response&gt;

它们的效果分别为:

那么,如何才能保存多个Cookie值呢?

 

问题解答

在网络中搜索答案,最后突然明白,可以在一个Set Cookie Header中设置多个Value,这样就可以保存多个Cookie。

示例代码

&lt;!--
    IMPORTANT:
    - Policy elements can appear only within the &lt;inbound&gt;, &lt;outbound&gt;, &lt;backend&gt; section elements.
    - To apply a policy to the incoming request (before it is forwarded to the backend service), place a corresponding policy element within the &lt;inbound&gt; section element.
    - To apply a policy to the outgoing response (before it is sent back to the caller), place a corresponding policy element within the &lt;outbound&gt; section element.
    - To add a policy, place the cursor at the desired insertion point and select a policy from the sidebar.
    - To remove a policy, delete the corresponding policy statement from the policy document.
    - Position the &lt;base&gt; element within a section element to inherit all policies from the corresponding section element in the enclosing scope.
    - Remove the &lt;base&gt; element to prevent inheriting policies from the corresponding section element in the enclosing scope.
    - Policies are applied in the order of their appearance, from the top down.
    - Comments within policy elements are not supported and may disappear. Place your comments between policy elements or at a higher level scope.
--&gt;
&lt;policies&gt;
    &lt;inbound&gt;
        &lt;base /&gt;
        &lt;set-variable name="token" value="@(context.Request.Body?.AsFormUrlEncodedContent(preserveContent: true)?["id_token"]?.Single())" /&gt;
        &lt;return-response&gt;
            &lt;set-status code="201" /&gt;
            &lt;set-header name="Set-Cookie" exists-action="override"&gt;
                &lt;value&gt;cookie0=000000&lt;/value&gt;
                &lt;value&gt;@("cookie1=" + context.Variables.GetValueOrDefault&lt;string&gt;("token", "no value"))&lt;/value&gt;
                &lt;value&gt;@("cookie2=" +"2222222")&lt;/value&gt;
                &lt;value&gt;cookie3=111111&lt;/value&gt;
            &lt;/set-header&gt;
            &lt;set-body&gt;@(context.Variables.GetValueOrDefault&lt;string&gt;("token", "no value"))&lt;/set-body&gt;
        &lt;/return-response&gt;
    &lt;/inbound&gt;
    &lt;backend&gt;
        &lt;base /&gt;
    &lt;/backend&gt;
    &lt;outbound&gt;
        &lt;base /&gt;
    &lt;/outbound&gt;
    &lt;on-error&gt;
        &lt;base /&gt;
    &lt;/on-error&gt;
&lt;/policies&gt;

测试效果

 

HTTP Cookie(也叫 Web Cookie 或浏览器 Cookie)是服务器发送到用户浏览器并保存在本地的一小块数据。浏览器会存储 cookie 并在下次向同一服务器再发起请求时携带并发送到服务器上。

服务器收到 HTTP 请求后,服务器可以在响应标头里面添加一个或多个 Set-Cookie 选项。浏览器收到响应后通常会保存下 Cookie,并将其放在 HTTP Cookie 标头内,向同一服务器发出请求时一起发送。

 

参考资料

创建Cookie: https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Cookies#%E5%88%9B%E5%BB%BA_cookie

How to use APIM set-header policy to manage Set-Cookie headers : https://learn.microsoft.com/en-us/answers/questions/1390333/how-to-use-apim-set-header-policy-to-manage-set-co

 

正在加载评论...