-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for getting and setting the cookies #4
Comments
Yes, the WebResourceRequested event might work as a work around but it is not ideal. I'll make sure cookies are on our backlog for APIs. Thanks. |
Can you say what you would want to use this API for? Is it managing auth related cookies? |
@david-risney yes, mainly auth- and session cookies. Basically, whenever you build a desktop application that hosts the browser control to interact with a specific known site (or web application product) and you also need to do some out-of-band communication stuff, e.g. uploading/downloading content or doing status messages, calling web services, etc, you need to be able to share the authentication data. One example would be to build a rich-client desktop integration for an issue tracker or build server, where you can login to the web server and then also are able to expose the attachements or build artifacts directly to the desktop. |
Hopefully support all under Chrome's Dev Tools's Application Storage. But the highlighted 3 hopefully be there.
|
Yes please! It's very important to improve the ability to list, read, set, and clear cookies, especially when WebView2 uses a separate-process mode, and it is a major problem currently! Please see my explanation in the section "Inability to download PDF files because of cookie access failure" in issue #38. Although the following cookie issues are in the repo for WPF WebView 1, these same problems are also applicable to WebView2. When designing WebView2, we would greatly appreciate it if you could solve these cookie difficulties:
|
WebResourceRequested usually doesn't work for reading cookies because IWebView2WebResourceRequestedEventArgs.Response is always null because it's not intended for apps to read, rather it's intended for apps to intercept a Request and generate a different Response. To collect cookies, you'd typically need to look for When a website is viewed in WebView2 and then you try to use Although I mentioned |
The cookie problem is our highest priority currently -- it is a major problem in our environment. Would it be helpful if I start discussing the precise details of the proposed cookie API? I suggest adding to IWebView2WebView:
Interface Each cookie in the
In our environment, it's much less important to support
I also suggest making the following methods in
If it's too difficult to support all of those, then at least the |
I would like to point out that Anyway what about other platforms? With Xamarin and .Net Core |
We also have a requirement for this. We need to get customisation/authentication cookies on a per URL basis to send in web crawler requests. Use case:
On macOS we use |
Any updates? |
Nothing concrete to report yet. But cookie management is a popular feature request and one we will be looking into. |
I also need cookie management so that I can add an auth cookie to the request being sent to the website I am navigating to. It has been suggested in this discussion that it might be possible to set cookies in the WebResourceRequested event, but there seems to be a bug in WebResourceRequested that ignores attempts to set the headers (despite the documentation saying you can do it). For example, this fails to add a cookie:
Another example of WebResourceRequested failing to allow headers to be set, this time involving trying to set the Authentication property: If you inspect the headers immediately after trying to set them, you will see that nothing has been set. So I think we must wait for a cookie management feature to be added to WebView2, or the bugs in WebResourceRequested to be fixed. Is there any estimate of when either of those is likely to be addressed please? Many thanks. |
Cookies can be accessed from the Chrome "Cookie" database with the Chrome DevTools Protocol (C++ example):
This method can be used for any of the protocols Chrome supports: https://chromedevtools.github.io/devtools-protocol/tot/ You can specifically use "Network.clearBrowserCookies", "Network.deleteCookies", "Network.getAllCookies", "Network.getCookies", "Network.setCookie", and "Network.setCookies" for cookie management. |
Thank you @killerbobjr, will be trying this one! |
@killerbobjr I was able to test with with the WinForms implemetnation and it worked as advertised. Thank you for the tip! Follow-Up question: The developer tools can be disabled via policy and are then no longer available from within the hosted browser control as well. This blocks the cookie access via devleoper tools for corporate scenarios. I created issue #331 for this. |
@MichaelKetting Is there any chance you could post a code snippet please to show how you got it to work with WinForms? |
@pipalot You call |
Yes, this is a stop-gap until the official API is ready. |
@MichaelKetting, that part for getting cookies using this.WebView2.CoreWebView2.CallDevToolsProtocolMethodAsync(...) has been done, thanks again in addition also looking to intercept External URI call Like mailto:// , want to intercept before the popup comes, and re-direct for appropriate application within the app. Since everything is within application. |
@vmeganathan81 ah, okay, now I understand. That's a separate topic I can't contribute to. I would recommend starting a new topic to get the right answers since this topic is just about the cookies-api. |
Can you post a code example please? I always get this error and have no idea whats the issue
|
@Symbai
|
@frankdekker Thank you, it turned out that |
CDP has a couple of quirks like that where one parameter is "optional", but only if one of the other "optional" parameters is given. You can open issues on CDP here through crbug: https://bugs.chromium.org/p/chromium/issues/entry?components=Platform%3EDevTools%3EPlatform |
// cc @peiche-jessica Hey folks. We're implementing cookie management APIs in WebView2 and have an API proposal up. If you have any feedback or thumbs-up, let us know in the proposal! P.S. if you haven't already, you can watch the MicrosoftEdge/WebView2Announcements repo to subscribe to WV2 announcements, new API proposals, etc. |
@liminzhu |
@ShaunLoganOracle thanks for the kind words. The cookie manager API is in the latest Pre-release package, the expectation is that it will land in Release package in the next release. We ship SDKs about the same cadence as Edge browser, and normally it's every 6 weeks, but for the next one it will be around 1/20 ish give and take a few days due to end of year and all that :). |
+1 to the people waiting for these APIs to be GAed :)
|
This is now available in our latest release package 1.0.705.50. Thanks! |
I have click once application that uses webview2 and every time the user update the app, the application deletes the cookies. I checked the folder for webview2 and sure the cookies are deleted. How can i prevent that from happening during an update. |
Hey @markismail7 - The cookies are tied to a User Data Folder. The default location for that folder is next to the exe, so likely as ClickOnce is updating it's deleting that folder and you lose cookies. To get around this, can you try specifying a different location for the User Data Folder when initializing the WebView2? More info: |
Is there an example on how to set WebView2 folder ? |
Assuming you are using .NET (the Win32 pattern is similar) you can set the user data folder when creating the environment: And use this environment object to call |
I really appreciate the response here. It didn't do anything. I've been working on this for hours now. The only way around it for me, is to copy the folder before updating and paste it back after updating. I wish it was easier than that. I was looking into it over the web. Alot of people are struggling to do this. |
@champnic Hi here I can see that set cookies in WebVeiw2 is resolved. But here I am still failed to set the cookies through webresourcerequested event handler. Correct me if I made any mistakes in my code, using VB.net and Webview version is 1.0.1020.30 1.Initialized handler in CoreWebView2InitializationCompleted
2.Setting headers in CoreWebView2_WebResourceRequested
While loading CoreWebView2_WebResourceRequested is firing and these headers are setting there Thanks in advance. |
Hey @shyjusachin - This function will only set the cookie as a header when sending a request to the server. It is not saving the cookie locally. You can use the cookie APIs if you want to do that: |
Thank you @champnic for the quick reply. As per your response what I understood is based on my above code, it will send whatever the Cookies required for the domain. Here I am not used CookieManager Api so that it will not save into local storage. Still my webview functionality is not working as expected, even here I am sending cookies with the header, url is redirected to login page due to lack of cookies availability. Please let me know any more information required my side. Expecting your feedback on it. Thank you |
This sounds like it might be a mismatch between what is being sent as a cookie, and what the server is expecting. You can verify what cookies are being sent by looking in the DevTools Network tab, or by looking at a NetLog: |
Is there any way to get (not set) a cookie from the WebView2 instance? |
@ShaunLoganOracle @thepwrtank18 Yes you can use any of it based on your requirement eg : |
Thanks; i'll will have a look into this. |
I looked at the API docs (https://docs.microsoft.com/en-us/microsoft-edge/hosting/webview2/reference/iwebview2webview) but didn't see find a matching API, so here's the feature request:
It would be great to be able to get and set the cookies used by the WebView. Internet Explorer allows getting the cookies by calling InternetGetCookieEx() from wininet.dll.
With this feature you build an application that uses both the browser control and a side channel when interacting with a website.
Edit: Possibly, the WebResourceRequested event might allow access via the raw headers from the GetResponse method (https://docs.microsoft.com/en-us/microsoft-edge/hosting/webview2/reference/iwebview2webresourcerequestedeventargs#interface_i_web_view2_web_resource_requested_event_args) but that's quite the effort.
AB#27223320
The text was updated successfully, but these errors were encountered: