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

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

问题描述

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

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

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

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

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

它们的效果分别为:

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

 

问题解答

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

示例代码

<!--
    IMPORTANT:
    - Policy elements can appear only within the <inbound>, <outbound>, <backend> 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 <inbound> 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 <outbound> 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 <base> element within a section element to inherit all policies from the corresponding section element in the enclosing scope.
    - Remove the <base> 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.
-->
<policies>
    <inbound>
        <base />
        <set-variable name="token" value="@(context.Request.Body?.AsFormUrlEncodedContent(preserveContent: true)?["id_token"]?.Single())" />
        <return-response>
            <set-status code="201" />
            <set-header name="Set-Cookie" exists-action="override">
                <value>cookie0=000000</value>
                <value>@("cookie1=" + context.Variables.GetValueOrDefault<string>("token", "no value"))</value>
                <value>@("cookie2=" +"2222222")</value>
                <value>cookie3=111111</value>
            </set-header>
            <set-body>@(context.Variables.GetValueOrDefault<string>("token", "no value"))</set-body>
        </return-response>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

测试效果

 

附录:介绍HTTP Cookie

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

相关文章
|
19天前
|
存储 JavaScript 前端开发
Cookie 反制策略详解:Cookie加解密原理、Cookie和Session机制、Cookie hook、acw_sc__v2、jsl Cookie调试、重定向Cookie
Cookie 反制策略详解:Cookie加解密原理、Cookie和Session机制、Cookie hook、acw_sc__v2、jsl Cookie调试、重定向Cookie
39 1
|
20天前
|
存储 C#
【Azure APIM】APIM 策略语句如何读取请求头中所携带的Cookie信息并保存为变量
【Azure APIM】APIM 策略语句如何读取请求头中所携带的Cookie信息并保存为变量
|
20天前
|
Web App开发 JSON 数据格式
【Azure Developer】浏览器查看本地数据文件时遇见跨域问题(CORS)
【Azure Developer】浏览器查看本地数据文件时遇见跨域问题(CORS)
【Azure Developer】浏览器查看本地数据文件时遇见跨域问题(CORS)
|
20天前
|
Web App开发 编解码 监控
【Azure 媒体服务】Azure Media Player 在Edge浏览器中不能播放视频问题的分析与解决
【Azure 媒体服务】Azure Media Player 在Edge浏览器中不能播放视频问题的分析与解决
|
20天前
|
存储 API 网络架构
【Azure 存储服务】MP4视频放在Azure的Blob里面,用生成URL在浏览器中打开之后,视频可以正常播放却无法拖拽视频的进度
【Azure 存储服务】MP4视频放在Azure的Blob里面,用生成URL在浏览器中打开之后,视频可以正常播放却无法拖拽视频的进度
|
21天前
|
Java 应用服务中间件 nginx
【Azure 环境】Azure应用程序网关设置set_Cookie=key=value; SameSite=Strict; HTTPOnly,AzureAD登录使用cookie时使用不了的案例记录
【Azure 环境】Azure应用程序网关设置set_Cookie=key=value; SameSite=Strict; HTTPOnly,AzureAD登录使用cookie时使用不了的案例记录
|
28天前
|
机器学习/深度学习 人工智能 前端开发
【人工智能】利用TensorFlow.js在浏览器中实现一个基本的情感分析系统
使用TensorFlow.js在浏览器中进行情感分析是一个非常实用的应用场景。TensorFlow.js 是一个用于在JavaScript环境中训练和部署机器学习模型的库,使得开发者能够在客户端直接运行复杂的机器学习任务。对于情感分析,我们可以使用预先训练好的模型来识别文本中的积极、消极或中性情感。
49 4
【人工智能】利用TensorFlow.js在浏览器中实现一个基本的情感分析系统
|
19天前
|
编解码 JavaScript 前端开发
JS逆向浏览器脱环境专题:事件学习和编写、DOM和BOM结构、指纹验证排查、代理自吐环境通杀环境检测、脱环境框架、脱环境插件解决
JS逆向浏览器脱环境专题:事件学习和编写、DOM和BOM结构、指纹验证排查、代理自吐环境通杀环境检测、脱环境框架、脱环境插件解决
39 1
|
1月前
|
自然语言处理 资源调度 JavaScript
JS 逆向基础篇:JS作用域和浏览器对象属性补环境
JS 逆向基础篇:JS作用域和浏览器对象属性补环境
52 1
|
30天前
|
JavaScript
js怎样控制浏览器前进、后退、页面跳转
js怎样控制浏览器前进、后退、页面跳转
44 0