none
C# Stay logged in while navigating RRS feed

  • Question

  • Hello,

    I'm having trouble staying connected while navigating with the WebBrowser object.

    I figured out how to log in and everything seems to work just fine, but whenever I navigate to another page I get disconnected.

    I believe that what I need to do is to keep the cookies, from page to page, but I can't quite figure out how.

    Note that I'm using the WebBrowser object to log in and not the HttpWebRequest.

    Thanks!

    Wednesday, March 28, 2012 8:11 PM

Answers

  • Then it makes no sense that going from one page to the other via the webbrowser doesn't maintain the Login state.  If the website you are going to works perfectly using IE9, then putting a webbrowser into your application will do the exact same thing becase that Webbrowser IS the same thing, it is an actual instance of the IE9 webbrower.  So in this case you have some "other" problem.  Maybe you can explain more.

    JP Cowboy Coders Unite!


    • Edited by Mr. Javaman II Thursday, March 29, 2012 4:04 PM
    • Proposed as answer by Dummy yoyo Friday, March 30, 2012 8:29 AM
    • Marked as answer by Dummy yoyo Thursday, April 5, 2012 2:26 AM
    Thursday, March 29, 2012 3:07 PM

All replies

  • There is an object called a CookieContainer which you persist behind the scenes. When you visit a page and you get the cookies, you put them into the 'jar'. When you go out to request a new page, you associate your HttpWebRequest with the cookie jar and then you'll stay logged in.

    There's a good example of this here: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.cookiecontainer.aspx

    Wednesday, March 28, 2012 9:48 PM
  • The CookieContainer appears to only belong to the request. Should I instead keep the CookieCollection from response.Cookies?

    Wouldn't the following code return an empty cookie jar?

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://google.com");

    request.CookieContainer = new CookieContainer();

    CookieContainer cool = request.CookieContainer;



    • Edited by Khailan Wednesday, March 28, 2012 10:26 PM
    Wednesday, March 28, 2012 10:25 PM
  • No no, the cookie container has nothing to do with the problem.  You said you are using the Webbrwoser and NOT the HTTPWebRequest.  Right?

    JP Cowboy Coders Unite!

    Wednesday, March 28, 2012 10:31 PM
  • That's right, I'm using the WebBrowser to log in the website.
    Thursday, March 29, 2012 12:13 AM
  • Then it makes no sense that going from one page to the other via the webbrowser doesn't maintain the Login state.  If the website you are going to works perfectly using IE9, then putting a webbrowser into your application will do the exact same thing becase that Webbrowser IS the same thing, it is an actual instance of the IE9 webbrower.  So in this case you have some "other" problem.  Maybe you can explain more.

    JP Cowboy Coders Unite!


    • Edited by Mr. Javaman II Thursday, March 29, 2012 4:04 PM
    • Proposed as answer by Dummy yoyo Friday, March 30, 2012 8:29 AM
    • Marked as answer by Dummy yoyo Thursday, April 5, 2012 2:26 AM
    Thursday, March 29, 2012 3:07 PM
  • May be a moot point, but worth talking about.

    The way I've used Cookies with HttpWebRequest was the following: the very first request should not be a post, and just something to get the cookie names. Subsequent requests use the same logic.

    CookieContainer cc = new CookieContainer();
    HttpWebRequest wrc = (HttpWebRequest) WebRequest.Create(URL);
    wrc.CookieContainer = cc;
    wrc.AllowAutoRedirect = false;
    if (postData.Length > 0) //post data is any form content in the form of controlname=value&controlname2=value2...
    {
        wrc.Method = "POST";
        wrc.ContentType = "application/x-www-form-urlencoded";
        wrc.ContentLength = postData.Length;
        StreamWriter sr = new StreamWriter(wrc.GetRequestStream());
        sr.Write(postData);
        sr.Flush();
        sr.Close();
        sr.Dispose();
    }
    HttpWebResponse respc = (HttpWebResponse) wrc.GetResponse();
    Response res = new Response(new StreamReader(respc.GetResponseStream()).ReadToEnd(), respc.StatusCode.ToString());
    foreach (Cookie cook in respc.Cookies)
    {
        if (cook.Domain.StartsWith("."))
        {
            cook.Domain = cook.Domain.Substring(1);
        }
        cc.Add(cook);
    }
    //Use response text to do whatever iwth the data.
    res.ResponseText

    Thursday, March 29, 2012 3:40 PM
  • your internet explorer settings may be affecting the control, check whether you have disabled cookies in IE

    mark my post as answer if my post was usefull to you

    Friday, March 30, 2012 10:33 AM
  • I don't understand why but showing MessageBox in between navigating sites solves this problem for me. When I try to navigate few pages, it normally signs me out. But if I place: MessageBox.Show("aaaa"); after logging in, it does not.

    Saturday, June 16, 2012 12:26 PM