locked
Set Cookies in a Webbrowser control RRS feed

  • Question

  • Hi All,

    I have a problem regarding webbrowser control in windows phone 8. I have a Native Login page in the app where we login user through service. In response of the login service we get a token.

    Now I have a page with WebBrowser control.In web browser control we open a url which has login page and dashboard. As the user is already logged in and we have the token. So we need to navigate user directly to dashboard instead of the login page.

    To do this in webbrowser control we need to set Cookies in control. How can we do this task?

    Please help me as we need to close this issue asap. Thanking you in advance.

    Wednesday, February 4, 2015 4:05 PM

Answers

  • I have a very similar authentication flow in my app.  Using native calls (HttpClient) I am able to retrieve the authentication token, and can easily set a cookie with that token, and use the cookie with subsequent native calls via HttpClient.

    I believe that what you are asking for is the ability to add that cookie (with the token you got natively) into the webbrowser control's cookie store BEFORE trying to load your dashboard page.  You want your webbrowser control's request for the dashboard page to provide the cookie so that the user doesn't have to log in again, and only one webbrowser request is made.  Is this correct? 

    Sadly, I do not think this is possible.  The other answers describe how to add a cookie to a webbrowser control's page after that page has been loaded.  This is adding the cookie to the "document" within the browser control, not into the webbrowser control's store.  These approaches will not allow you to add the cookie into the webbrowser control's store, ahead of time, so that it will be sent when you first request the page. 

    If I am correct about what you are asking for (I want this too!) it is not supported on Windows Phone as you cannot write directly to the webbrowser control's cookie store.  You can read it, but not write to it.  It's sad as this particular authentication flow is very common, and easy to achieve on both iOS and Android... just not on Windows Phone.

    To get around this in my app, I perform my native authentication (HttpClient) and then authenticate again within the webbrowser control itself.  Basically, load the login page into the browser, then submit the login form via eval of JavaScript, and when that finishes loading (valid response), load the actual target page.  This is a fragile and inefficient hack, but it seems to work. 

    Hope this helps...

    Monday, February 9, 2015 8:01 PM
  • Hi Dineshrawat0508,

    Based on my knowledge, your scenario should be like this one. http://stackoverflow.com/questions/13287409/windows-phone-webbrowser-set-cookies. Please try the method on the above link.

    Please let me know if this is not helpful.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Thursday, February 5, 2015 6:06 AM
  • 1,You shoud set the property IsScriptEnabled is true;

    2,on the WebBrowser's LoadCompleted eventhander execute this code:

     string script = "window.setCookie=function(){document.cookie = 'uid=abc'}";
                            webBrowser.InvokeScript("eval", script);
                            webBrowser.InvokeScript("setCookie");

    Thursday, February 5, 2015 6:22 AM

All replies

  • Hi Dineshrawat0508,

    Based on my knowledge, your scenario should be like this one. http://stackoverflow.com/questions/13287409/windows-phone-webbrowser-set-cookies. Please try the method on the above link.

    Please let me know if this is not helpful.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Thursday, February 5, 2015 6:06 AM
  • 1,You shoud set the property IsScriptEnabled is true;

    2,on the WebBrowser's LoadCompleted eventhander execute this code:

     string script = "window.setCookie=function(){document.cookie = 'uid=abc'}";
                            webBrowser.InvokeScript("eval", script);
                            webBrowser.InvokeScript("setCookie");

    Thursday, February 5, 2015 6:22 AM
  • I have a very similar authentication flow in my app.  Using native calls (HttpClient) I am able to retrieve the authentication token, and can easily set a cookie with that token, and use the cookie with subsequent native calls via HttpClient.

    I believe that what you are asking for is the ability to add that cookie (with the token you got natively) into the webbrowser control's cookie store BEFORE trying to load your dashboard page.  You want your webbrowser control's request for the dashboard page to provide the cookie so that the user doesn't have to log in again, and only one webbrowser request is made.  Is this correct? 

    Sadly, I do not think this is possible.  The other answers describe how to add a cookie to a webbrowser control's page after that page has been loaded.  This is adding the cookie to the "document" within the browser control, not into the webbrowser control's store.  These approaches will not allow you to add the cookie into the webbrowser control's store, ahead of time, so that it will be sent when you first request the page. 

    If I am correct about what you are asking for (I want this too!) it is not supported on Windows Phone as you cannot write directly to the webbrowser control's cookie store.  You can read it, but not write to it.  It's sad as this particular authentication flow is very common, and easy to achieve on both iOS and Android... just not on Windows Phone.

    To get around this in my app, I perform my native authentication (HttpClient) and then authenticate again within the webbrowser control itself.  Basically, load the login page into the browser, then submit the login form via eval of JavaScript, and when that finishes loading (valid response), load the actual target page.  This is a fragile and inefficient hack, but it seems to work. 

    Hope this helps...

    Monday, February 9, 2015 8:01 PM