【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
【Azure APIM】APIM 策略语句如何来设置多个Cookie值让浏览器保存的更多相关文章
- 【Azure API 管理】APIM CORS策略设置后,跨域请求成功和失败的Header对比实验
在文章"从微信小程序访问APIM出现200空响应的问题中发现CORS的属性[terminate-unmatched-request]功能"中分析了CORS返回空200的问题后,进一 ...
- Azure Terraform(十)利用 Azure DevOps 的条件语句选择发布环境
一,引言 之前我讲过的所有的案例中,都是将整个Azure Resource 部署到同一个订阅下,没有做到灵活的在 Azure Pipeline 在运行前选择需要部署的环境.在实际的项目开发中,我们也会 ...
- 使用Azure Policy(策略)强制实现资源Tag的坑
Azure的Tag(标记)可以帮助运维人员对云资源分类从而方便地进行计费和资源管理.然而在具体实践中工程师部署云资源的时候常常会忘记给资源做标记打Tag. 针对这个问题,Azure的官方文档建议是可以 ...
- python学习:利用循环语句完善输入设置
利用循环语句完善输入设置 使用for循环: 代码1:_user = "alex"_password = "abc123" for i in range(3): ...
- 11.best fields策略(dis_max参数设置)
主要知识点 常规multi-field搜索结果分析 dis_max参数设置 一.为帖子数据增加content字段 POST /forum/article/_bulk { "u ...
- windows防火墙实验-命令行设置远程桌面连接以及禁止浏览器上网
windows防火墙实验-设置远程桌面连接以及禁止浏览器上网 实验环境: 1.win2008远程桌面服务 2.win7-1 10.10.10.136 3.win7-2 10.10.10.153 实验步 ...
- Redis设置内存最大占用值
Redis设置内存最大占用值: Redis设置占用物理机最大的内存 #占用最大20G maxmemory 20480mb Redis设置内存装不下了,有限删除即将过期的 当前已用内存超过maxmemo ...
- if(response.isSuccess){}else{}的方式,如果我们由于忽略没有设置success字段的值,就可能导致
在日常开发中,我们会经常要在类中定义布尔类型的变量,比如在给外部系统提供一个RPC接口的时候,我们一般会定义一个字段表示本次请求是否成功的. 关于这个”本次请求是否成功”的字段的定义,其实是有很多种讲 ...
- 用javascript设置和读取cookie的例子
请看下面用javascript设置和读取cookie的简单例子,现在的问题是,如果要设置的是一个cookie集,比如在cookie1集中有uname,uid两组信息,应该如何写呢?cookie(&qu ...
- 设置UITableView的separatorInset值为UIEdgeInsetsZero,分隔线不最左端显示的问题
一.问题描述 UITableView分割线要显示到最左端 查看UITableView的属性,发现设置separatorInset的值可以自定义分割线的位置. @property (nonatomic) ...
随机推荐
- redis基本数据类型 List
127.0.0.1:6379> LPUSH test a (integer) 1 127.0.0.1:6379> LPUSH test b (integer) 2 127.0.0.1:63 ...
- Redis中的缓存雪崩、缓存击穿、缓存穿透问题
1. 什么是缓存雪崩 当我们提到缓存系统中的问题,缓存雪崩是一个经常被讨论的话题.缓存雪崩是指在某一时刻发生大量的缓存失效,导致瞬间大量的请求直接打到了数据库,可能会导致数据库瞬间压力过大甚至宕机.尤 ...
- Flask后端开发(一)-基础知识和前期准备
目录 1.背景介绍 1.1. 项目背景 1.2. 项目难点 1.3. 项目环境 2. flask后端开发实现的功能 3. flask部署和前后端对接 3.1. flask运行配置和服务器部署 3.2. ...
- Unity csc.rsp文件
试验版本 Unity2020.3 编译符号,就是#if UNITY_ANDROID这个东西,在处理多平台多分支的情况下挺有用的,但是在ProjectSettings里面设置比较麻烦,不主动代码调用保存 ...
- Unity3D 选择焦点切换
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- FreeSWITCH的moh使用笔记
操作系统 :CentOS 7.6_x64 FreeSWITCH版本 :1.10.9 之前写过FreeSWITCH安装的文章,今天整理下moh使用过程中遇到的问题及解决方案,并提供moh音频下载途径.F ...
- springboot项目在docker中运行
前端时间需要把项目打包到docker中运行,于是就让组员去探索,最后整个过程是这样的. 首先我们做java开发,一般都是使用springboot开发,开发完成,我们需要把springboot项目打包成 ...
- 【随手记】python免api调用谷歌翻译
pip3 install googletrans==4.0.0-rc1 from googletrans import Translator translator = Translator() tra ...
- 关于RS485通讯TVS器件选择的经验
先说经验结论 如果你的RS485用于频繁热拔插, 比如作为手持终端使用, 且手持器与目标板非隔离, 那么使用6.8CA可能是更好的选择. 因为有热拔插会产生浪涌, 而且在非隔离的场合有些工业设备接地也 ...
- 国企项目就用国产的 Solon Java Framework,v2.5.12 发布
Solon 是什么框架? Java 新的生态级应用开发框架.国产.从零开始构建,有自己的标准规范与开放生态(历时五年,具备全球第二级别的生态规模).与其他框架相比,解决了两个重要的痛点:启动慢,费内存 ...