morphinapg
morphinapg

Reputation: 109

How to set a cookie properly in AngleSharp?

I'm writing an app to automate grabbing data from my profile on the website Criticker.com. I'm able to successfully log into the website with AngleSharp, and after I do so, I grab the cookie, but when I try to reuse it later, it doesn't seem to work.

After I sign in to the site, I grab the cookie using:

Cookie = document.Cookie;

which I then save in a file to retrieve the next time I load the app

So when connecting to the site next time, I use this code:

context = BrowsingContext.New(AngleSharp.Configuration.Default.WithDefaultLoader().WithJs().WithDefaultCookies());

if (Cookie is not null)
    context.SetCookie(new Url("https://www.criticker.com"), Cookie);


var document = await context.OpenAsync("https://www.criticker.com");

However, when I check, I can tell I'm not still signed in, even though when I use the site on chrome, I stay signed in for a month.

When the document loads, I do see the cookie listed in document.Cookie, so it seems like setting it worked, but why isn't it logged in then when the page loads?

Upvotes: 0

Views: 359

Answers (1)

Florian Rappl
Florian Rappl

Reputation: 3189

For cookies I'd recommend to use AngleSharp.Io. This has a much better implementation for cookies - the core AngleSharp library just has a wrapper around the .NET cookie store, which, unfortunately is not good.

After installing AngleSharp.Io (https://github.com/AngleSharp/AngleSharp.Io) you can create your config like

var config = AngleSharp.Configuration.Default.WithDefaultLoader().WithJs().WithTemporaryCookies();

Make sure that the cookie value also has a Path=/ / the full value usually returned from the Set-Cookie response header.

Upvotes: 0

Related Questions