如何清除Windows Phone 8 cordova插件中的cookie? [英] How can I clear the cookies in a windows phone 8 cordova plugin?

查看:236
本文介绍了如何清除Windows Phone 8 cordova插件中的cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何清除windows phone 8 cordova插件中的Cookie。



我一直在玩这里的建议 http://cloudstore.blogspot.in/2010/ 09 / clearing-cookies-on-windows-phone-7-or.html ,此处 http://developer.nokia.com/community/wiki/Integrate_Facebook_to_Your_Windows_Phone_Application



这基本上是您获取Web浏览器实例并使用方便的方法,像这样:

  await browserInstance.ClearCookiesAsync(); 

或者您从请求中获取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 Cookie)
{
cookie.Discard = true;
cookie.Expired = true;
}

我的问题是两折:



1)我不知道如何获取cordova浏览器实例以尝试调用ClearCookiesAsync()方法



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);

所以基本上,如果我能找出如何从cordova获取浏览器的一个实例,插件,或者清除HTTPClient中的Cookie,这将是确定的。



编辑:有趣... 如何在Windows Phone 8中获取HttpOnly Cookie?



显然,你可以通过HttpClientHandler访问cookies,所以我的代码开头就是这样...

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

...但是创建一个新的Cookie容器似乎没有清除Cookie? >

这看起来很有趣:努力尝试在.net 4.5中使用HttpClient来响应cookie



但确实从HttpResponseMessage检索Cookie,但试图使用cookie.Discard和cookie.Expired使这些cookie无效。



它看起来好像答案是你必须得到cordova浏览器实例来丢弃cookie,上面的只会让你玩个人响应中的cookie似乎是一种只读方式:(



相似/重复的问题:



从phonegap插件运行windows手机访问cookies或WebBroswer类
在Cordova上的ClearCookiesAsync()

解决方案

我发现更简单的方法是使用一个基于服务器的注销网址,它将通过设置相同的cookie过期的过期设置cookie使您的cookie无效。



基本上,虽然android和ios可以做我想要的,它看起来像你不能轻易做到使用windows phone API,因为我发现的API是在那里,但很难通过一个cordova插件,因为


中解释的原因

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

这篇关于如何清除Windows Phone 8 cordova插件中的cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆