Welcome Guest Search | Active Topics | Sign In | Register

Separate cookies between web browsers in same app and same site Options
Jim B
Posted: Saturday, January 31, 2015 5:41:39 PM
Rank: Advanced Member
Groups: Member

Joined: 1/31/2015
Posts: 70
Hello,

We are planning on switching from another browser component (CefSharp) , possibly to EO.WebBrowser. Our application uses multiple browser instances and performs automations to all of them. All browser instances perform on the same site. What I would like, would be to have separate cookies for each browser instance, so that the navigational states don't get messed up.

Is this possible?

Kind regards,

Jim

EDIT: I just discovered this post http://www.essentialobjects.com/forum/postst8667_Session-gmail-login-separated-in-two-webview-diferent.aspx so I suppose this is how it's done.
eo_support
Posted: Monday, February 2, 2015 11:07:38 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Jim B wrote:


Yes. Separating them in different AppDomain is the correct way to do it. All WebView objects inside a single AppDomain shares the same "cookie jar". So it is not possible to isolate the cookies without isolating the WebViews into different AppDomain first.

Thanks!
yvesm
Posted: Monday, February 2, 2015 1:42:26 PM
Rank: Advanced Member
Groups: Member

Joined: 12/1/2014
Posts: 57
I found that posting but I have not figured out yet how to apply this to a WebView yet. If anyone could post a bit more details that would be great.
eo_support
Posted: Monday, February 2, 2015 1:49:10 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Quote:
I found that posting but I have not figured out yet how to apply this to a WebView yet. If anyone could post a bit more details that would be great.


You will need to search online about how to use multiple AppDomains. That is a generic .NET programming topic that would not have anything to do with our product. One you are familiar with that, the code in the previous thread should look very simple and straight forward to you.
Jim B
Posted: Monday, February 2, 2015 2:45:22 PM
Rank: Advanced Member
Groups: Member

Joined: 1/31/2015
Posts: 70
Having said that, I would prefer a more webview-specific cookie management option. Something in the constructor maybe.
eo_support
Posted: Monday, February 2, 2015 4:32:42 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Hi,

It is technically possible for us to add this in a future build that would allow you to have separate "browser spaces", all WebViews inside the same browser space would share cookie, history, etc. This would allow you to implement features similar to Chrome's incognito mode. However that may not be much easier than using .NET's native AppDomain to implement the same. Nevertheless, even though conceptually they are very similar, the actual code should be easier because you do not have to deal with cross AppDomain issue.

Thanks!
Jim B
Posted: Tuesday, February 3, 2015 8:40:02 AM
Rank: Advanced Member
Groups: Member

Joined: 1/31/2015
Posts: 70
Maybe a chapter in the "Advanced Topics" section would work until you can make time for this API enhancement. For example, a version of the MultiTabBrowser example that you already have would prove the technology.

Personally I find the helper class above a good place to start but I also a) don't understand and b) don't know how e.g. I can have multiple views (or webcontrols ???) running events, javascript etc from multiple places in my wpf code. Of course, you don't "have to" give such examples, it would only be helpful.

On the other hand... is there any way to intercept and maybe rename cookies? So that we could use existing API calls to add e.g. a prefix to the cookies created by one view and other prefix for the other???
eo_support
Posted: Tuesday, February 3, 2015 10:51:16 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Hi,

To understand the helper class above, you need to search for AppDomain online. You will find plenty of articles on how to use it.

Currently there are several ways for you to examine/intercept cookies:

1. You can implement your own custom resource handler. This will allow you to attach cookies to the Request object. See here for more details:
http://www.essentialobjects.com/doc/6/advanced/resource_handler.aspx
2. You can handle BeforeSendHeaders and AfterReceiveHeaders event to examine the cookies that are sent to the server and the cookies that are sent to you from the server;

It is possible for you to implement your own "cookie jar" by:

1. Handle AfterReceiveHeaders event to examine all the cookies sent to you from the server and save them in your own "cookie jar";
2. Implement your own custom resource handler, inside your resource handler you can examine your "cookie jar" and attach the corresponding cookie to the Request object;

This would be a lot of additional code than just using multiple AppDomains though.

Hope this helps. Please feel free to let us know if you still have any questions.

Thanks!
Jim B
Posted: Tuesday, February 3, 2015 1:14:09 PM
Rank: Advanced Member
Groups: Member

Joined: 1/31/2015
Posts: 70
Now that's a nice idea to try! I will take a shot and get back with the results.

Thank you for the pointer!

Regards,

Jim
fm
Posted: Wednesday, February 4, 2015 7:58:40 AM
Rank: Newbie
Groups: Member

Joined: 7/2/2013
Posts: 7
I would also like to see an official way to have separate "browser spaces" in EO.WebBrowser. Currently we are trying to utilize AppDomains to have incognito-like behavior with different WebView instances in same process, and it is PITA! Especially when you encounter types in EO namespace which are neither [Serializable] nor MarshalByRefObject.
eo_support
Posted: Wednesday, February 4, 2015 1:31:39 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
fm wrote:
I would also like to see an official way to have separate "browser spaces" in EO.WebBrowser. Currently we are trying to utilize AppDomains to have incognito-like behavior with different WebView instances in same process, and it is PITA! Especially when you encounter types in EO namespace which are neither [Serializable] nor MarshalByRefObject.


Thanks for the feedback. We will put this one on our list.
Kyriakos
Posted: Monday, February 16, 2015 11:04:42 AM
Rank: Newbie
Groups: Member

Joined: 2/13/2015
Posts: 5
eo_support wrote:

......
It is possible for you to implement your own "cookie jar" by:

1. Handle AfterReceiveHeaders event to examine all the cookies sent to you from the server and save them in your own "cookie jar";
2. Implement your own custom resource handler, inside your resource handler you can examine your "cookie jar" and attach the corresponding cookie to the Request object;

...



Hello
I am trying the previous solution and i have same questions.
In the documentation for "custom resource handler" you say that "There can be multiple thread calling ProcessRequest for different Request object at the same time;". Is it right to use lock for synchronization into this function because when calling my url the code is executed multiple times (1 for favicon 1 for the html).
I am also wondering if by doing something like this
string url = request.Url;

System.Net.WebRequest req = System.Net.HttpWebRequest.Create(url);
System.Net.WebResponse resp = req.GetResponse();
StreamReader reader = new StreamReader(resp.GetResponseStream());
string responseText = reader.ReadToEnd();


response.Write(responseText);
is the only way to call the url with data already provided in the request-parameter in our custom resource handler
eo_support
Posted: Monday, February 16, 2015 1:25:58 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Hi Kyriakos,

You will have to pretty much answer both questions yourself. For the lock question, you have to answer what you are locking it for. Multi-thread does not automatically equals lock. If you have two threads does completely totally different and unrelated things and don't share anything together, then obviously you don't need any lock/synchronization between them. If they do share things, then you do need to coordinate between them. So whether you need such synchronization and how to do it would be totally up to you.

For the second one the browser engine does not care how you get your contents. For example, you can return a plain "hello" text for any Url the browser engine asks you and that is completely legal. So once again, this part is completely up to you.

Thanks!
Jim B
Posted: Tuesday, February 17, 2015 8:14:44 AM
Rank: Advanced Member
Groups: Member

Joined: 1/31/2015
Posts: 70
Would the "cookie-jar" approach work with https connections?
eo_support
Posted: Tuesday, February 17, 2015 10:02:48 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Jim B wrote:
Would the "cookie-jar" approach work with https connections?


It should. By the time the corresponding events are fired, all the cookies should have already been decrypted.

Thanks!
Jim B
Posted: Tuesday, February 17, 2015 10:10:05 AM
Rank: Advanced Member
Groups: Member

Joined: 1/31/2015
Posts: 70
I tried the following:

Code: C#
void web_view_AfterReceiveHeaders(object sender, EO.WebBrowser.ResponseEventArgs e)
        {
            Debug.WriteLine("getting cookies from response...");
            CookieCollection cc = e.Response.Cookies;
            foreach (string name in cc)
            {
                Debug.WriteLine("response cookie:"+name + "=" + cc[name]);
            }
        }

        void web_view_BeforeSendHeaders(object sender, EO.WebBrowser.RequestEventArgs e)
        {
            Debug.WriteLine("sending cookies with request...");

            CookieCollection cc = e.Request.Cookies;
            foreach (string name in cc)
            {
                Debug.WriteLine("request cookie:" + name + "=" + cc[name]);
            }
        }


in order to inspect the incoming / outgoing cookies. I see that it is not ok to just override the cookie collection of the request object at the "BeforeSendHeaders" point. So, if we implement the resourcehandler, what's the resource type of a cookie? Or am I thinking it wrong?

Regards,

Jim
eo_support
Posted: Tuesday, February 17, 2015 10:27:50 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Hi Jim,

Cookies ride with HTTP request/response (They are in the HTTP header), so it does not have its own resource. A resource of any type (HTML, image, JavaScript, etc) can carry cookies. When you use custom resource handler, you will get the Request and Response objects. You would examine/set cookies through those two objects.

Thanks!
Jim B
Posted: Tuesday, February 17, 2015 10:57:07 AM
Rank: Advanced Member
Groups: Member

Joined: 1/31/2015
Posts: 70
So, essentially the Response would be the original response + custom cookie ?

Code: C#
class TestHandler : ResourceHandler
    {
        public override bool Match(Request request)
        {
            return true;
        }

        public override void ProcessRequest(Request request, Response response)
        {
             //how do you keep the original web_view response (i.e. the actual original server response) and add e.g. a custom cookie? Is this an overkill in order to repeat the original request?

            string url = request.Url;

            HttpWebRequest newRequest = (HttpWebRequest)WebRequest.Create(url);

            HttpWebResponse newResponse = (HttpWebResponse)newRequest.GetResponse();

            Stream resStream = newResponse.GetResponseStream();

            response.OutputStream.Write(resStream);  //or something like that?
        }
    }


Also, I see here http://www.essentialobjects.com/forum/postst8402_cookies-and-proxies.aspx that "If you do not wish to use custom resource handler, you can attach your own cookies by handling the WebView's BeforeRequestLoad event:

http://www.essentialobjects.com/doc/6/eo.webbrowser.webview.beforerequestload.aspx

This allows you to assign cookies to each request."

Is this approach also a valid choice?

Kind regards,

Jim
eo_support
Posted: Tuesday, February 17, 2015 11:50:27 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Hi,

Yes. BeforeRequestLoad should work and maybe an easier solution for you than implementing custom resource handler. For a custom resource handler you will need to do much more rather than just copy the response. Specifically, you must set various HTTP header entries correctly (particularly ContentType and ContentEncoding). With BeforeRequestLoad you can use e.Request.Cookies to attach your own cookies.

Thanks!
Jim B
Posted: Tuesday, February 17, 2015 11:55:44 AM
Rank: Advanced Member
Groups: Member

Joined: 1/31/2015
Posts: 70
Hey,

Thanks for the quick reply.

I set a breakpoint in BeforeRequestLoad and I see that e.Request.Cookies is always empty (AllKeys is size 0). Shouldn't I see the existing already-prepared-and-ready-to-send cookies? So that I can drop existing stuff and introduce new cookies?

Cheers,

Jim


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.