The ability to set cookies via Cookies GetAsync has disappeared. #1042

Closed
opened 2026-01-29 16:56:23 +00:00 by claunia · 3 comments
Owner

Originally created by @S-tokarev on GitHub (Dec 22, 2025).

In the previous version, I used this method for setting cookies:
var cookies = await window.WebContents.Session.Cookies.GetAsync(new CookieFilter());
In this version, I didn't see direct access to interacting with cookies. There's no documentation on Session, and in the code, I only see OnChanged, not Get/Set.
How can I set cookies in WebContents now, and will this be included in future versions? thx

Originally created by @S-tokarev on GitHub (Dec 22, 2025). In the previous version, I used this method for setting cookies: `var cookies = await window.WebContents.Session.Cookies.GetAsync(new CookieFilter());` In this version, I didn't see direct access to interacting with cookies. There's no documentation on `Session`, and in the code, I only see `OnChanged`, not `Get/Set`. How can I set cookies in `WebContents` now, and will this be included in future versions? thx
claunia added the enhancementapi labels 2026-01-29 16:56:23 +00:00
Author
Owner

@AeonSake commented on GitHub (Dec 22, 2025):

While there currently is no direct API on the C# side, you can always just invoke setting the cookies on the JS side via a JS call:

await window.WebContents.ExecuteJavaScriptAsync<string>("document.cookie='key=value; expires=Mon, 22 Dec 2025 12:00:00 UTC; path=/';");

Or with a helper/extension method like this:

public static class WebContentsExtensions
{
    public static async Task SetCookie(this WebContents webContents, string name, string value, DateTime? expires = null, string? path = null)
    {
        var sb = new StringBuilder();
        sb.Append(name).Append('=');
        if (string.IsNullOrEmpty(value)) //if no value is provided, the cookie is deleted
        {
            sb.Append(value);
        }
        if (expires.HasValue)
        {
            sb.Append("; expires=");
            sb.Append(expires.Value.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss UTC", CultureInfo.InvariantCulture));
        }
        if (!string.IsNullOrEmpty(path))
        {
            sb.Append("; path=").Append(path);
        }

        await webContents.ExecuteJavaScriptAsync<string>($"document.cookie='{sb}';");
    }
}

There is probably a much better way to do this while utilizing the Cookie class that already exists in ElectronNET.API.Entities (including proper serialization/deserialization of all possible cookie properties), and probably also needs another method to retrieve cookies or a specific cookie. But until that is implemented, this approach should work fine.

@AeonSake commented on GitHub (Dec 22, 2025): While there currently is no direct API on the C# side, you can always just invoke setting the cookies on the JS side via a JS call: ```cs await window.WebContents.ExecuteJavaScriptAsync<string>("document.cookie='key=value; expires=Mon, 22 Dec 2025 12:00:00 UTC; path=/';"); ``` Or with a helper/extension method like this: ```cs public static class WebContentsExtensions { public static async Task SetCookie(this WebContents webContents, string name, string value, DateTime? expires = null, string? path = null) { var sb = new StringBuilder(); sb.Append(name).Append('='); if (string.IsNullOrEmpty(value)) //if no value is provided, the cookie is deleted { sb.Append(value); } if (expires.HasValue) { sb.Append("; expires="); sb.Append(expires.Value.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss UTC", CultureInfo.InvariantCulture)); } if (!string.IsNullOrEmpty(path)) { sb.Append("; path=").Append(path); } await webContents.ExecuteJavaScriptAsync<string>($"document.cookie='{sb}';"); } } ``` There is probably a much better way to do this while utilizing the `Cookie` class that already exists in `ElectronNET.API.Entities` (including proper serialization/deserialization of all possible cookie properties), and probably also needs another method to retrieve cookies or a specific cookie. But until that is implemented, this approach should work fine.
Author
Owner

@FlorianRappl commented on GitHub (Dec 22, 2025):

I am actually unsure why this was removed (I think it was just an oversight - seemed to have happened with the transition to ElectronNET.Core).

I am trying to re-add the API currently. Should be available in 0.4.1.

@FlorianRappl commented on GitHub (Dec 22, 2025): I am actually unsure why this was removed (I think it was just an oversight - seemed to have happened with the transition to ElectronNET.Core). I am trying to re-add the API currently. Should be available in 0.4.1.
Author
Owner

@FlorianRappl commented on GitHub (Dec 23, 2025):

Part of most recent preview (0.4.1-pre.23).

@FlorianRappl commented on GitHub (Dec 23, 2025): Part of most recent preview (0.4.1-pre.23).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#1042