问题描述

在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值让浏览器保存的更多相关文章

  1. 【Azure API 管理】APIM CORS策略设置后,跨域请求成功和失败的Header对比实验

    在文章"从微信小程序访问APIM出现200空响应的问题中发现CORS的属性[terminate-unmatched-request]功能"中分析了CORS返回空200的问题后,进一 ...

  2. Azure Terraform(十)利用 Azure DevOps 的条件语句选择发布环境

    一,引言 之前我讲过的所有的案例中,都是将整个Azure Resource 部署到同一个订阅下,没有做到灵活的在 Azure Pipeline 在运行前选择需要部署的环境.在实际的项目开发中,我们也会 ...

  3. 使用Azure Policy(策略)强制实现资源Tag的坑

    Azure的Tag(标记)可以帮助运维人员对云资源分类从而方便地进行计费和资源管理.然而在具体实践中工程师部署云资源的时候常常会忘记给资源做标记打Tag. 针对这个问题,Azure的官方文档建议是可以 ...

  4. python学习:利用循环语句完善输入设置

    利用循环语句完善输入设置 使用for循环: 代码1:_user = "alex"_password = "abc123" for i in range(3): ...

  5. 11.best fields策略(dis_max参数设置)

    主要知识点 常规multi-field搜索结果分析 dis_max参数设置     一.为帖子数据增加content字段     POST /forum/article/_bulk { "u ...

  6. windows防火墙实验-命令行设置远程桌面连接以及禁止浏览器上网

    windows防火墙实验-设置远程桌面连接以及禁止浏览器上网 实验环境: 1.win2008远程桌面服务 2.win7-1 10.10.10.136 3.win7-2 10.10.10.153 实验步 ...

  7. Redis设置内存最大占用值

    Redis设置内存最大占用值: Redis设置占用物理机最大的内存 #占用最大20G maxmemory 20480mb Redis设置内存装不下了,有限删除即将过期的 当前已用内存超过maxmemo ...

  8. if(response.isSuccess){}else{}的方式,如果我们由于忽略没有设置success字段的值,就可能导致

    在日常开发中,我们会经常要在类中定义布尔类型的变量,比如在给外部系统提供一个RPC接口的时候,我们一般会定义一个字段表示本次请求是否成功的. 关于这个”本次请求是否成功”的字段的定义,其实是有很多种讲 ...

  9. 用javascript设置和读取cookie的例子

    请看下面用javascript设置和读取cookie的简单例子,现在的问题是,如果要设置的是一个cookie集,比如在cookie1集中有uname,uid两组信息,应该如何写呢?cookie(&qu ...

  10. 设置UITableView的separatorInset值为UIEdgeInsetsZero,分隔线不最左端显示的问题

    一.问题描述 UITableView分割线要显示到最左端 查看UITableView的属性,发现设置separatorInset的值可以自定义分割线的位置. @property (nonatomic) ...

随机推荐

  1. 「luogu - P4313」文理分科 Mincut

    link. Pretty nice practice for the min-cut trick. Starting out we eliminate the constraint that if f ...

  2. mpi转以太网连接200plc以太网监控同时与步科触摸屏通信

    西门子PLC200 226PLC转以太网通过PPI-ETH-XD1.0集中采集不占用编程口同时与步科触摸屏通信 现有设备及联网要求客户车间内有6台纺机设备,控制系统采用西门子PLC,型号为CPU226 ...

  3. 国庆微信头像DIY:轻松打造个性化头像

    前言 国庆节马上要到了,今天就教你如何从0到1使用canvas生成国庆风微信头像. 本文包含以下内容: vue3项目搭建,需求分析 canvas合成图片原理 github自动化部署 开发过程遇到的问题 ...

  4. 前端三件套系例之CSS——响应式布局

    文章目录 1.什么是响应式设计 1-1 定义 1-2 响应式设计的优势 2.屏幕的相关概念 3.viewport 视口 3-1 什么是viewport 3-2 设置viewport 4.媒体查询 @m ...

  5. CMake中添加 -lpthread 编译参数

    问题:当在linux命令行中编译关于进程/线程的源文件时,需要加上 -lpthread 参数动态链接线程库而在CMake中如何加入呢? 方法:只需在 add_executable() 命令前面加上以下 ...

  6. Hello-FPGA CoaXPress 2.0 FPGA HOST IP Core PCIe Demo User Manual

    目录 1 说明 4 2 设备连接 7 3 VIVADO FPGA工程 8 4 调试说明 9 图 1‑1 资料目录 4 图 1‑2 VIVADO工程目录结构 5 图 1‑3 VS软件工程目录 5 图 1 ...

  7. 虹科干货 | 零售业数智升级不掉队,get数据,get未来!

    电商崛起,传统零售行业危机四伏,全渠道盈利与可持续化成为难点,库存管理这块难啃的"硬骨头"也同样让零售商倍感压力...... 背腹受敌的零售商,如何才能在数字化转型道路上避免利润缩 ...

  8. "科来杯"第十届山东省大学生网络安全技能大赛决赛复现WP

    从朋友那里得来的附件,感觉题目有意思,简单复现一下 MISC 简单编码 1.题目信息 0122 061 1101011 0172 0122 0105 061 1011010 0127 0154 014 ...

  9. 系统RAM几乎爆满与解决方法

    先说一遍,遇事不决就重启! 在电脑长时间运转下某些无良应用程序会产生大量的临时文件(目前我怀疑是有道云笔记) 最终导致系统爆炸 附图 在这种情况下,我下载了RAM实时监测我内存占用情况 结果发现pag ...

  10. 聊聊RNN&LSTM

    RNN 用于解决输入数据为,序列到序列(时间序列)数据,不能在传统的前馈神经网络(FNN)很好应用的问题.时间序列数据是指在不同时间点上收集到的数据,这类数据反映了某一事物.现象等随时间的变化状态或程 ...