2

I store authentication cookie in to a staic CookieContainer in my application, thanks to the answer on this link (1)

I implemented the code to get the CookieContainer on DocumentCompleted event :

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if (!this.webBrowser1.Document.Title.Replace(" ", string.Empty).ToLower().Contains("xxx"))
        {
            CookieContainer ck =  GetUriCookieContainer(this.webBrowser1.Url);

            validSession = ck;

            Succeeded = true;
            this.Close();
        }
    }

Because I'm using WebClient, I need to create an extension of it so it can store CookieContainer :

public class WebClientEx : WebClient
{
    public WebClientEx()
        : this(new CookieContainer())
    { }

    public WebClientEx(CookieContainer c)
    {
        this.CookieContainer = c;
    }

    public CookieContainer CookieContainer { get; set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);

        var castRequest = request as HttpWebRequest;
        if (castRequest != null)
        {
                castRequest.CookieContainer = this.CookieContainer;
        }

        return request;
    }
}

After storing the CookieContainer from (1), I'm able to request any content of the page without authenticating :

        WebClientEx e = new WebClientEx(validSession);
        string x = e.UploadString(new Uri("http://localhost:14590/default.aspx"), "ola");
        // x now contains the html page of default.aspx

The problem is when I tried to upload a file to the server using UploadFileAsync, I will try to describe the issue step-by-step :

  • Open the application
  • Open login form (default url will navigate to default.aspx page)
  • Login to the website with WebBrowser control
  • The webBrowser will navigate to default.aspx page
  • Store the cookie
  • Upload a file using UploadFileAsync method
  • The result (e.result) on UploadFileCompleted event is the login page's html content, meaning the CookieContainer which I passed in WebClient does not work.

But please look at these steps, the Upload function will work without a problem :

  • Open the application
  • Open login form (default url will navigate to default.aspx page)
  • The webBrowser will navigate to default.aspx page
  • Store the cookie
  • Open login form, now the webBrowser will navigate directly to the default.aspx because the authenticated session is still available.
  • Upload a file using UploadFileAsync method -> success
  • I'm sure the CookieContainer which I get at the first login attempt is correct, because I'm able to use WebClient.UploadString() or any HttpWebRequest to the server with it, by the way I checked the CookieContainer on the second attemp and it's identical to the first.

I can't understand why this is happening? Do you have any ideas why? Please note that If I use other method such as WebClient.UploadString() there's no redirecting back to the login page if the cookieContainer is available.

Community
  • 1
  • 1
EagerToLearn
  • 675
  • 4
  • 18

1 Answers1

0

For some unknown reasons, the CookieContainer causes this problem, after watching fiddler, I realize the first call from webclient to the server did not contain any cookie, so I get rid of the CookieContainer and replace it with this :

    WebClient e = new WebClient();
    e.Headers.Add(HttpRequestHeader.Cookie, "cookies I get from webbrowser control");

And voila, everything is fine!

EagerToLearn
  • 675
  • 4
  • 18