经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » 浏览器 » 查看文章
【Azure APIM】APIM 策略语句如何来设置多个Cookie值让浏览器保存
来源:cnblogs  作者:路边两盏灯  时间:2024/1/5 9:08:09  对本文有异议

问题描述

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

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

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

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

  1. <return-response>
  2. <set-status code="201" />
  3. <set-header name="Set-Cookie" exists-action="override">
  4. <value>cookie0=000000</value>
  5. </set-header>
  6. <set-header name="Set-Cookie" >
  7. <value>@("cookie1=" + context.Variables.GetValueOrDefault<string>("token", "no value"))</value>
  8. </set-header>
  9. <set-body>@(context.Variables.GetValueOrDefault<string>("token", "no value"))</set-body>
  10. </return-response>

它们的效果分别为:

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

 

问题解答

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

示例代码

  1. <!--
  2. IMPORTANT:
  3. - Policy elements can appear only within the <inbound>, <outbound>, <backend> section elements.
  4. - 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.
  5. - 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.
  6. - To add a policy, place the cursor at the desired insertion point and select a policy from the sidebar.
  7. - To remove a policy, delete the corresponding policy statement from the policy document.
  8. - Position the <base> element within a section element to inherit all policies from the corresponding section element in the enclosing scope.
  9. - Remove the <base> element to prevent inheriting policies from the corresponding section element in the enclosing scope.
  10. - Policies are applied in the order of their appearance, from the top down.
  11. - Comments within policy elements are not supported and may disappear. Place your comments between policy elements or at a higher level scope.
  12. -->
  13. <policies>
  14. <inbound>
  15. <base />
  16. <set-variable name="token" value="@(context.Request.Body?.AsFormUrlEncodedContent(preserveContent: true)?["id_token"]?.Single())" />
  17. <return-response>
  18. <set-status code="201" />
  19. <set-header name="Set-Cookie" exists-action="override">
  20. <value>cookie0=000000</value>
  21. <value>@("cookie1=" + context.Variables.GetValueOrDefault<string>("token", "no value"))</value>
  22. <value>@("cookie2=" +"2222222")</value>
  23. <value>cookie3=111111</value>
  24. </set-header>
  25. <set-body>@(context.Variables.GetValueOrDefault<string>("token", "no value"))</set-body>
  26. </return-response>
  27. </inbound>
  28. <backend>
  29. <base />
  30. </backend>
  31. <outbound>
  32. <base />
  33. </outbound>
  34. <on-error>
  35. <base />
  36. </on-error>
  37. </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

 

原文链接:https://www.cnblogs.com/lulight/p/17946065

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号