简体   繁体   English

如何清除Windows Phone 8 Cordova插件中的Cookie?

[英]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. 我试图弄清楚如何清除Windows Phone 8 cordova插件中的cookie。

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 我一直在这里http://cloudstore.blogspot.in/2010/09/clearing-cookies-on-windows-phone-7-or.html和这里http://developer.nokia.com/社区/ 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: 或者您从请求中获取Cookie并丢弃/过期它们,如下所示:

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 1)我不知道如何获取cordova浏览器实例才能尝试调用ClearCookiesAsync()方法

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: 2)我不使用HTTPWebRequest对象,因为我需要访问权限来设置一些标头信息,我使用的是HTTPClient对象和HTTPRequestMessage,即:

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. 因此,基本上,如果我能弄清楚如何在插件中从cordova获取浏览器实例,或者从HTTPClient清除cookie,就可以了。

Edit: Interesting... How can I get HttpOnly cookies in Windows Phone 8? 编辑:有趣的... 如何在Windows Phone 8中获取HttpOnly cookie?

Apparently you can get access to cookies via the HttpClientHandler, so the start of my code above would be like this... 显然,您可以通过HttpClientHandler来访问cookie,所以上面我的代码的开始就是这样...

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? ...但是创建新的Cookie容器似乎无法清除Cookie?

This also looks interesting: Struggling trying to get cookie out of response with HttpClient in .net 4.5 这看起来也很有趣: 在.net 4.5中使用HttpClient努力使cookie失去响应

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. 我使用了上面的链接,该链接确实确实从HttpResponseMessage中检索了cookie,但是尝试使用cookie.Discard和cookie.Expired使这些cookie无效。

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 :( 似乎答案是您必须让cordova浏览器实例丢弃cookie,以上内容仅会让您以似乎是只读的方式使用来自各个响应的cookie:

Similar/Duplicate questions: 相似/重复的问题:

Access to cookies or WebBroswer class from phonegap plugin running windows phone ClearCookiesAsync() on Cordova 通过 在Cordova上 运行Windows Phone ClearCookiesAsync()的 phonegap插件访问cookie或WebBroswer类

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. 我发现执行此操作的更简单方法是使用基于服务器的“注销” URL,该URL将设置cookie具有过去的过期时间,从而使cookie无效。

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 基本上,尽管android和ios可以满足我的要求,但是您似乎无法使用Windows Phone API轻松完成此操作,因为我发现该API“在”那里,但是由于以下原因,它很难通过cordova插件进行访问在问题中解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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