简体   繁体   English

如何从UWP WebView提取httponly cookie?

[英]How to extract httponly cookies from UWP WebView?

I know there are techniques to retrieve WebView cookies via HttpBaseProtocolFilter.CookieManager which is shared between WebView and HttpClient at least withing the borders of the same applications. 我知道有一些通过HttpBaseProtocolFilter.CookieManager检索WebView cookie的技术,该技术至少在相同应用程序边界的情况下在WebView和HttpClient之间共享。 However it does not give access to httponly cookies. 但是,它不提供对httponly cookie的访问。 httponly cookies also are not shared between WebView and HttpClient. 在WebView和HttpClient之间也不会共享httponly cookie。 Frankly speaking I understand the restrictions but before I give up I'd like to ask if anybody solved this problem? 坦白地说,我了解这些限制,但在放弃之前,我想问问是否有人解决了这个问题?

This is what I need. 这就是我所需要的。 We have a web site written in Java. 我们有一个用Java编写的网站。 The site generates JSESSION httponly cookie and passes it to the client. 该站点生成JSESSION httponly cookie并将其传递给客户端。 We are working on an UWP application where we instantiate WebView which plays the role of the client. 我们正在开发一个UWP应用程序,在该应用程序中实例化充当客户端角色的WebView。 Then, we inject javascript to communicate between UWP code and the displayed page. 然后,我们注入javascript以在UWP代码和显示的页面之间进行通信。 Everything works perfect as expected. 一切都按预期完成。 Now we need to do a few requests from UWP code but in the context of the current session. 现在,我们需要在当前会话的上下文中,从UWP代码中进行一些请求。 For that we need to use the same JSESSION for HttpClient in UWP but I'm unable to find a way to do that. 为此,我们需要对UWP中的HttpClient使用相同的JSESSION,但是我无法找到一种方法来做到这一点。

Every time I send a request from UWP a new JSESSION is generated and that prevents the logic from working right. 每当我从UWP发送请求时,都会生成一个新的JSESSION,这将阻止逻辑正常工作。 So, is there any way to either retrieve that httponly cookie from WebView or override it with the one which is assigned to the HttpClient? 因此,有什么方法可以从WebView检索该httponly cookie或使用分配给HttpClient的cookie替代它吗?

If you use Windows.Web.Http.HttpClient and WebView in UWP, the cookies are automatically shared through the app's context between the HttpClient and the WebView, including the HttpOnly cookies. 如果您在UWP中使用Windows.Web.Http.HttpClient和WebView,则cookie会通过应用程序的上下文在HttpClient和WebView之间自动共享,包括HttpOnly cookie。

To validate it, I used the c# code on this document to create a HttpOnly cookie when the asp.net web page is loaded. 为了验证它,我在加载asp.net网页时使用了本文档中的c#代码创建HttpOnly cookie。 Then, I made a UWP app and added a WebView on XAML page to view the website, in code-behind, I used the following code to get the cookie: 然后,我制作了一个UWP应用,并在XAML页面上添加了WebView来查看网站,在代码背后,我使用以下代码来获取Cookie:

HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
HttpCookieCollection cookieCollection = filter.CookieManager.GetCookies(new Uri("http://localhost:xxxxx/Default.aspx"));
foreach (var cookie in cookieCollection)
{
    Debug.WriteLine(cookie.Name+": "+cookie.Value+" HttpOnly: "+cookie.HttpOnly);
}

You could see the screenshot. 您可以看到屏幕截图。 I can get the HttpOnly cookie: 我可以得到HttpOnly cookie:

在此处输入图片说明

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

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