Restsharp 响应中缺少 Cookie,但 Postman 中没有

Cookie missing in Restsharp response but not in Postman

我能够使用 Postman 并使用 Postman 通过 WebAPI 成功登录并为以下请求获取 cookie。

我从 Postman 获取代码并使用 RestSharp 输入 Visual Studio C#。然而,使用 Visual Studio 和 RestSharp,请求返回成功 ok 200,但没有 COOKIE。

我错过了什么?我已经在网上看了 4 天了。

感谢您的帮助。以下是来自 Postman 的 C# 代码:

var client = new RestClient("https://client.awebsite.ca/user/login?_format=hal_json");
var request = new RestRequest(Method.POST);
request.AddHeader("Postman-Token", "a16887c6-a1da-fa25-e721-621c4b19318b");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Content-Type", "text/plain");
request.AddParameter("undefined", "{\"name\":\"firstname.lastname\", \"pass\":\"passwordoffirstnamelastname\"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

您似乎没有创建 CookieContainer

参见:https://github.com/restsharp/RestSharp/wiki/Cookies

var client = new RestClient("https://client.awebsite.ca/user/login?_format=hal_json");
client.CookieContainer = new System.Net.CookieContainer();

// Your request code...

IRestResponse response = client.Execute(request);