Mohammad Roshani
Mohammad Roshani

Reputation: 769

Remove cookie from GeckoFx C#

I want to remove cookie of a site in Geckofx FireFox Browser. I find this

Xpcom.QueryInterface<nsICookieManager>((object)Xpcom.GetService<nsICookieManager>("@mozilla.org/cookiemanager;1")).Remove(...);

and

[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface nsICookieManager
{
    void RemoveAll();
    nsISimpleEnumerator GetEnumeratorAttribute();
    void Remove(nsAUTF8StringBase aHost, nsACStringBase aName,    nsAUTF8StringBase aPath, bool aBlocked);
}

I am confused with the parameter of it, for example: nsAUTF8StringBase

 public class nsAUTF8StringBase : IString
{
    protected nsAUTF8StringBase();

    protected static int NS_CStringContainerFinish(nsAUTF8StringBase container);
    protected static int NS_CStringContainerInit(nsAUTF8StringBase container);
    protected static int NS_CStringGetData(nsAUTF8StringBase str, out IntPtr data, IntPtr nullTerm);
    protected static bool NS_CStringGetIsVoid(nsAUTF8StringBase str);
    protected static int NS_CStringSetData(nsAUTF8StringBase str, byte[] data, int length);
    protected static void NS_CStringSetIsVoid(nsAUTF8StringBase str, bool isVoid);
    public virtual void SetData(string value);
    public override string ToString();
}

how i should create "aHost"? and how i can remove a site cookie?

Upvotes: 2

Views: 986

Answers (1)

Bartosz
Bartosz

Reputation: 4786

I see three ways which you could try

1 - If you know the cookie name, host and path, you can do the following (instantiate the string parameters):

 nsICookieManager cookieMan = Xpcom.GetService<nsICookieManager>("@mozilla.org/cookiemanager;1");
 cookieMan = Xpcom.QueryInterface<nsICookieManager>(cookieMan);
 cookieMan.Remove(new nsAUTF8String("SomeHost"),new nsACString("SomeName"), new nsAUTF8String("SomePath"),false);

2 - use the Gecko.CookieManager static class which can remove the cookies based on plain string parameters (see details below)

3 - If you don't know all the info about the cookie you want to delete (you know only the value or name or something), then you can use the Gecko.CookieManager to enumerate the cookies and delete it.

        var cookies = CookieManager.GetEnumerator();

        while (cookies.MoveNext())
        {
            if (cookies.Current.Name == "CookieIHate")
            {
                CookieManager.Remove(cookies.Current.Host, cookies.Current.Name, cookies.Current.Path, false);
            }
        }

Upvotes: 1

Related Questions