Why does Restsharp returns different results in .net core vs.net framework?

Viewed 384

I have two "identical" console applications, one targeting .NET Framework 4.7.2, the other targeting net5.0.

In both applications I am using the exact same method, below a snippet:

var client = new RestClient("https://secure.xxxx.yy/login.aspx")
{
    Timeout = -1
};
var request = new RestRequest(Method.POST);
request.AddHeader("Connection", "keep-alive");
... //snip
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Cookies.FirstOrDefault(cookie => cookie.Name == "ASP.NET_SessionId")?.Value);

In the net5.0 version, response.Cookies contains 3 items (with the one I am looking for), but in the .NET Framework version, response.Cookies only contains 1 item (without the one I am looking for)...

What is the reason for this and what are my possibilities to have the same result in .NET Framework? I am trying to create an Excel VSTO plugin, so I think I am bound to .NET Framework, right?

Below the screenshots from within VS:

.net5.0 (good) response

.net framework (bad) response

Below the response from fiddler (they are identical for both .net versions):

HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=utf-8
Location: /xxxxx.eu/authorized/home.aspx
Server: Microsoft-IIS/7.5
Access-Control-Allow-Origin: *
Set-Cookie: ASP.NET_SessionId=0hslsdigt5kpha30rtk4veoc; path=/; HttpOnly; SameSite=Lax
X-AspNet-Version: 4.0.30319
Set-Cookie: .ASPNET=F1B8BEB09F20EF52B75BBA81B5535EC580B873E5A65F43AEC779C27C50730998582DC814DCAF194206636ACFBA706E2DE90371071F9AEB6B03E0C8ACF0BDE44CB3844230CC2143B2307EBB4889C12764167E15CA3240A74B430156205F6CD53AC8982EBE8EF3FE51E09862DF5AB15A7145F8451357515C48D3F3F09D48E7D890; path=/; HttpOnly; SameSite=Lax
X-Powered-By: ASP.NET
Date: Thu, 22 Apr 2021 11:40:28 GMT
Content-Length: 156
1 Answers

as @phuzi mentioned, the cookies are not available since they are set as httponly.

adding a cookiejar to the client, I am able to get the required cookie:

var site = new Uri("https://secure.xxxx.yy/login.aspx");
var client = new RestClient(site)

CookieContainer _cookieJar = new CookieContainer();
client.CookieContainer = _cookieJar;
//.... execute the request
var cookies = _cookieJar.GetCookies(site);
Related