简体   繁体   中英

How can I clear the cookies in a windows phone 8 cordova plugin?

I'm trying to figure out how to clear the cookies in a windows phone 8 cordova plugin.

I've been playing with the advice here http://cloudstore.blogspot.in/2010/09/clearing-cookies-on-windows-phone-7-or.html and here http://developer.nokia.com/community/wiki/Integrate_Facebook_to_Your_Windows_Phone_Application

which is basically you either get the web browser instance and use a convenience method, like so:

await browserInstance.ClearCookiesAsync();

or you get the cookies from the request and discard/expire them, like so:

HttpWebRequest _webRequest;
Uri uri = new Uri("https://blah.com");
CookieContainer _cookieContainer = new CookieContainer();
_webRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
_webRequest.CookieContainer = this._cookieContainer;
var cookies = this._cookieContainer.GetCookies(uri);
foreach (Cookie cookie in cookies)
{
   cookie.Discard = true;
   cookie.Expired = true;
}

My problem is two fold:

1) I don't know how to get the cordova browser instance in order to try the calling the ClearCookiesAsync() method

2) I'm not using an HTTPWebRequest object as I need access to set some header information, I'm using a HTTPClient object and a HTTPRequestMessage ie:

HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://blah.com");
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/html"));
client.DefaultRequestHeaders.Add("Some custom stuff","More custom stuff");
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get,"/something/hello.php");
HttpResponseMessage response = await client.SendAsync(req);
response.EnsureSuccessStatusCode();
string responseBody = string.Empty;
responseBody = await response.Content.ReadAsStringAsync();
Debug.WriteLine(responseBody);

So basically, if I could figure out how to get an instance of the browser from cordova whilst within a plugin, or, clear the cookies from a HTTPClient this would be ok.

Edit: Interesting... How can I get HttpOnly cookies in Windows Phone 8?

Apparently you can get access to cookies via the HttpClientHandler, so the start of my code above would be like this...

HttpClientHandler handler = new HttpClientHandler();
handler.CookieContainer = new CookieContainer();
HttpClient client = new HttpClient(handler);

...but creating a new Cookie Container doesn't seem to clear the cookies?

This also looks interesting: Struggling trying to get cookie out of response with HttpClient in .net 4.5

I played with the above link and that does indeed retrieve cookies from the HttpResponseMessage but trying to invalidate those cookies using cookie.Discard and cookie.Expired does not work.

It looks as if the answer is you have to get the cordova browser instance to discard the cookies, the above will only let you play with the cookies from the individual response in what would seem to be a read only manner :(

Similar/Duplicate questions:

Access to cookies or WebBroswer class from phonegap plugin running windows phone ClearCookiesAsync() on Cordova

I found the easier way of doing this is using a server based "logout" url that will set-cookie invalidate your cookie, by setting the same cookie with an expiry in the past.

Basically, although android and ios can do what I wanted, it looks like you can't easily do it using the windows phone API, as I found the API "is" there, but it's hard to access via a cordova plugin for the reasons explained in the question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM