问题描述

在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. windows 网络模拟工具分享

    [下载地址] Releases · jagt/clumsy · GitHub [介绍] 无需安装 无需篡改和代理 系统级限制,不针对单个程序,但可以针对单个IP 离线也可以限制,随停随用 界面简单 [ ...

  2. Modbus转Profinet网关改写变频器运行状态在1200PLC程序控制实例

    兴达易控Modbus转Profinet网关将丹佛斯变频器接入西门子 1200PLC 配置案例 案例简介: 本案例是兴达易控Modbus转Profinet网关连接丹佛斯变频器在西门子 1200PLC程序 ...

  3. fmpeg 多张图片转视频,视频转gif,截取视频,截取视频里的帧

    ffmpeg 多张图片转视频,视频转gif,截取视频,截取视频里的帧 月色下的独轮车 于 2017-06-20 17:32:01 发布 6250 收藏 3文章标签: ffmpeg版权 android学 ...

  4. Oracle 11g数据库详解(2017-01-23更新)

    Oracle 11g数据库详解 整理者:赤勇玄心行天道 QQ:280604597 Email:280604597@qq.com 大家有什么不明白的地方,或者想要详细了解的地方可以联系我,我会认真回复的 ...

  5. Kubernetes 中使用consul-template渲染配置

    Kubernetes 中使用consul-template渲染配置 当前公司使用consul来实现服务发现,如Prometheue配置中的target和alertmanager注册都采用了consul ...

  6. Java IO教程- Java文件

    创建文件 我们可以从中创建一个 File 对象 路径名 父路径名和子路径名 URI(统一资源标识符) 我们可以使用File类的以下构造函数之一创建一个文件: File(String pathname) ...

  7. linux系统centos7安装docker

    1.Docker官网安装地址 https://docs.docker.com/engine/install/centos/#prerequisites 2.离线安装下载地址 https://downl ...

  8. 题解 CF1264D1

    前言 数学符号约定: \(\dbinom{n}{m}\):表示 \(n\) 选 \(m\) . 如非特殊说明,将会按照上述约定书写符号. 题目分析: 考虑题目的问题弱一点的版本,假设此时我们的括号序列 ...

  9. 【Flutter】一文读懂混入类Mixin

    [Flutter]一文读懂混入类Mixin 基本介绍 Mixin是一种有利于代码复用,又避免了多继承的解决方案. Mixin 是面向对象程序设计语言中的类,提供了方法的实现,其他类可以访问 Mixin ...

  10. L2-040 哲哲打游戏

    这题读懂题目之后就发现它很呆 #include <bits/stdc++.h> using namespace std; const int N = 100010, M = 110; ve ...