C# 如何从Azure函数设置多个Cookie?
我正在返回一个C# 如何从Azure函数设置多个Cookie?,c#,cookies,azure-functions,C#,Cookies,Azure Functions,我正在返回一个HttpResponseMessage对象。我在我的回复上设置了多个cookie,就像这样 var cookie = new CookieHeaderValue($"a", "content of cookie a"); cookie.Expires = DateTimeOffset.Now.AddMonths(1); cookie.HttpOnly = true; cookie.Path = $"/
HttpResponseMessage
对象。我在我的回复上设置了多个cookie,就像这样
var cookie = new CookieHeaderValue($"a", "content of cookie a");
cookie.Expires = DateTimeOffset.Now.AddMonths(1);
cookie.HttpOnly = true;
cookie.Path = $"/l/";
cookies.Add(cookie);
cookie = new CookieHeaderValue($"test", "hello");
cookie.Expires = DateTimeOffset.Now.AddMonths(1);
cookie.HttpOnly = true;
cookie.Path = $"/l";
cookies.Add(cookie);
cookie = new CookieHeaderValue($"b", "content of cookie b");
cookie.Expires = DateTimeOffset.Now.AddMonths(1);
cookie.HttpOnly = true;
cookie.Path = $"/l/some-folder-path";
cookies.Add(cookie);
message.Headers.AddCookies(cookies);
看来我的函数正在正确设置标题。这是回应的截图
但是,在客户端,它只设置第一个cookie,而忽略其他两个cookie!我知道这一点,因为它只在下一个请求中发回cookiea
更神秘的是,它在下一个请求中发送回来的cookie完全没有出现在开发人员工具中
我正在对我的请求使用带有凭据的:true
我不明白为什么它只设置第一个cookie
提前谢谢
更新
我删除了路径
和HttpOnly
标志,但问题仍然存在
更新2
我试过你的例子,结果如下
这是响应标头,它在一个Set Cookie
标头中显示所有三个Cookie
但是Firefox只设置了一个cookie。Edge和Chrome中的相同问题
更新3
多亏了@Hury Shen,我找到了问题所在。Azure功能的第2版就是问题所在(这就是我的应用程序的基础)。版本2在一个Set Cookie
头中设置所有Cookie,而版本3为每个Cookie设置了单独的头(这是正确的方法)。要解决此问题,我必须迁移应用程序
由于您没有提供如何在代码中初始化变量
cookies
,因此我无法重现您的问题。但是我在函数中使用我的代码进行测试,它工作得很好。您可以参考以下我的代码:
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Net.Http.Headers;
public static async Task<HttpResponseMessage> Run(HttpRequest req, ILogger log)
{
var resp = new HttpResponseMessage();
var cookie1 = new CookieHeaderValue("name1", "content of cookie==1111");
cookie1.Expires = DateTimeOffset.Now.AddMinutes(1);
cookie1.HttpOnly = true;
cookie1.Path = "/l/";
var cookie2 = new CookieHeaderValue("name2", "content of cookie==2222");
cookie2.Expires = DateTimeOffset.Now.AddMinutes(1);
cookie2.HttpOnly = true;
cookie2.Path = "/l";
var cookie3 = new CookieHeaderValue("name3", "content of cookie==3333");
cookie3.Expires = DateTimeOffset.Now.AddMinutes(1);
cookie3.HttpOnly = true;
cookie3.Path = "/l/some-folder-path";
resp.Headers.AddCookies(new CookieHeaderValue[] {cookie1, cookie2, cookie3});
return resp;
}
#r“Newtonsoft.Json”
Net系统;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Extensions.Primitives;
使用Newtonsoft.Json;
使用System.Net.Http.Header;
公共静态异步任务运行(HttpRequest请求,ILogger日志)
{
var resp=新的HttpResponseMessage();
var cookie1=新的CookieHeaderValue(“name1”,“cookie的内容==1111”);
cookie1.Expires=DateTimeOffset.Now.AddMinutes(1);
cookie1.HttpOnly=true;
cookie1.Path=“/l/”;
var cookie2=新的CookieHeaderValue(“name2”,“cookie的内容==2222”);
cookie2.Expires=DateTimeOffset.Now.AddMinutes(1);
cookie2.HttpOnly=true;
cookie2.Path=“/l”;
var cookie3=新的CookieHeaderValue(“name3”,“cookie的内容==3333”);
cookie3.Expires=DateTimeOffset.Now.AddMinutes(1);
cookie3.HttpOnly=true;
cookie3.Path=“/l/some folder Path”;
resp.Headers.AddCookies(新的CookieHeaderValue[]{cookie1,cookie2,cookie3});
返回响应;
}
在我的浏览器中请求该功能后,我们可以在下面的屏幕截图中看到cookies:
========================================更新=============================
由于您没有提供如何在代码中初始化变量
cookies
,因此我无法重现您的问题。但是我在函数中使用我的代码进行测试,它工作得很好。您可以参考以下我的代码:
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Net.Http.Headers;
public static async Task<HttpResponseMessage> Run(HttpRequest req, ILogger log)
{
var resp = new HttpResponseMessage();
var cookie1 = new CookieHeaderValue("name1", "content of cookie==1111");
cookie1.Expires = DateTimeOffset.Now.AddMinutes(1);
cookie1.HttpOnly = true;
cookie1.Path = "/l/";
var cookie2 = new CookieHeaderValue("name2", "content of cookie==2222");
cookie2.Expires = DateTimeOffset.Now.AddMinutes(1);
cookie2.HttpOnly = true;
cookie2.Path = "/l";
var cookie3 = new CookieHeaderValue("name3", "content of cookie==3333");
cookie3.Expires = DateTimeOffset.Now.AddMinutes(1);
cookie3.HttpOnly = true;
cookie3.Path = "/l/some-folder-path";
resp.Headers.AddCookies(new CookieHeaderValue[] {cookie1, cookie2, cookie3});
return resp;
}
#r“Newtonsoft.Json”
Net系统;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Extensions.Primitives;
使用Newtonsoft.Json;
使用System.Net.Http.Header;
公共静态异步任务运行(HttpRequest请求,ILogger日志)
{
var resp=新的HttpResponseMessage();
var cookie1=新的CookieHeaderValue(“name1”,“cookie的内容==1111”);
cookie1.Expires=DateTimeOffset.Now.AddMinutes(1);
cookie1.HttpOnly=true;
cookie1.Path=“/l/”;
var cookie2=新的CookieHeaderValue(“name2”,“cookie的内容==2222”);
cookie2.Expires=DateTimeOffset.Now.AddMinutes(1);
cookie2.HttpOnly=true;
cookie2.Path=“/l”;
var cookie3=新的CookieHeaderValue(“name3”,“cookie的内容==3333”);
cookie3.Expires=DateTimeOffset.Now.AddMinutes(1);
cookie3.HttpOnly=true;
cookie3.Path=“/l/some folder Path”;
resp.Headers.AddCookies(新的CookieHeaderValue[]{cookie1,cookie2,cookie3});
返回响应;
}
在我的浏览器中请求该功能后,我们可以在下面的屏幕截图中看到cookies:
========================================更新=============================
您使用的是哪种浏览器?您好@Matt我在IE中测试了它。刚才我在Edge中测试了它,它似乎也可以正常工作。我的答案“更新”下的截图是我在Edge中的测试。嘿@hury shen我发现了这个问题。V2 Azure函数设置Cookie的方式与V3不同。我需要迁移应用程序以修复此问题。谢谢你的帮助。你用的是哪种浏览器?嗨@Matt我在IE中测试了它。我刚才在Edge中测试了它,它看起来也很好用。我的答案“更新”下的截图是我在Edge中的测试。嘿@hury shen我发现了这个问题。V2 Azure函数设置Cookie的方式与V3不同。我需要迁移应用程序以修复此问题。谢谢你的帮助。