I faced this situation many times where I have to transfer cookies from one WebBrowser control to other so that I can transfer the authentication and after searching I found a simple and straight forward solution for doing this.
Get Cookies from WebBrowser Control
After doing your operation like navigating to any page and login on the site you just have to write this code in any event:
string cookies = browser.Document.Cookies;
Above line of code will return a string containing all cookies in cookies string.
Now you can use this string to set cookies of other webbrowser control. But keep in mind that cookies string is found in Document property of WebBrowser control and document is only initialised after navigating to some URL. So before assigning these cookies either navigate to that domain who generated these cookies or set cookies while navigating.
Set Cookies in WebBrowser Control
There are two methods to set cookies in web browser control
1. This is very straight forward method to assign cookies in Cookie property of your WebBrowser’s document object:
wb.Document.Cookie = cookies;
This method may not work for all sites specially on sites running on https protocol.
2. As above method doesn’t guarantee to run on all sites so here is second method which you can use to set cookies:
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool InternetSetCookie(string lpszUrlName, string lpszCookieName, string lpszCookieData);
Cookie ck = new Cookie("key", "value", "/folder", "/");
InternetSetCookie("https://edunyte.com/folder", null, ck.ToString());
Now you can navigate to the URL specified in InternetSetCookie to read the cookie and verify that it is working.