问题描述

在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. Java 21 正式 GA,虚拟线程真的来了

    UTC 时间 2023 年 9 月 19 日,期盼已久的 Java 21 终于发布正式版! 本文一起来看看其中最受 Java 开发者关注的一项新特性:Loom 项目的两个新特性之一的 "虚拟 ...

  2. 为什么 Rust 备受开发者青睐?

    引子 作为一名敏锐的前端开发者,您可能早已对 Rust 有所耳闻,毕竟近几年,使用 Rust 开发的前端构建工具每经发布,其卓越的性能数据总是能带来社区的一阵惊叹. 图片来源:https://swc. ...

  3. 【Unity3D】Shader Graph简介

    1 Shader Graph 简介 ​ Shader Graph 是 Unity 官方在 2018 年推出的 Shader 制作插件,是图形化的 Shader 制作工具,类似于 Blender 中的 ...

  4. 《流畅的Python》 读书笔记 第三章字典和集合 20231017

    第3章 字典和集合 dict 类型是 Python 语言的基石 模块的命名空间.实例的属性和函数的关键字参数中都可以看到字典的身影 跟它有关的内置函数都在__builtins__.__dict__ 模 ...

  5. Chromium Trace and Perfetto使用详解

    1. Trace chromium 在 base 库中提供了 base::trace_event::TraceLog 类,该类是 TRACE_EVENT* , TRACE_COUNTER* 等宏的底层 ...

  6. Python自动化处理Excel数据

    需求描述:数据格式如下所示,需要分离出2023年7月1号之后的数据明细 数据核对与处理:从Excel文件中提取特定日期后的签收数据 1. 引言 在实际数据处理和分析过程中,经常会遇到需要从大量数据中提 ...

  7. C#.NET 国密SM4 CBC 对称加解密 与JAVA互通 ver:20231103

    C#.NET 国密SM4 CBC 对称加解密 与JAVA互通 ver:20231103 .NET 环境:.NET6 控制台程序(.net core). JAVA 环境:JAVA8,带maven 的JA ...

  8. c#利用反射获取枚举的信息

    1.将不同的枚举类型作为形参传入某函数内时,形参为Enum,在函数体内进行类型强转. private T GetEnumType<T>(object o) { T enumVal = (T ...

  9. Pooling与马赛克的秘密

      说到Pooling,相信学习过CNN的朋友们都不会感到陌生.Pooling在中文当中的意思是"池化",在神经网络当中非常常见,通常用的比较多的一种是Max Pooling,具体 ...

  10. LabVIEW基于机器视觉的实验室设备管理系统(2)

    目录 功能计划 普通用户 欢迎登录 信息查询 返回退出程序 效果演示 在前期的准备完成之后呢,就要开始实现我们的程序啦,不过在编程之前,我们一定要计划好这个系统需要什么功能. 功能计划 既然我们做的是 ...