Automatic user name and password authentication at HTML websites

| | No TrackBacks

HDB: This is lifted from some newsgroup. I can't remember where.

It would depend on how authentication is implemented on that site. If it is a custom authentication scheme you'll probably have to POST the username and password to a "login" page then keep track of a session variable to use on every subsequent Navigate.

If it's http authentication then you could pass the username and password in the URI: http://username:password@host/file.htm. Which can be done easily with the UriBuilder class. But, that's slowly becoming deprecated for obvious reasons.

Alternatively, with HTTP basic authentication, you just pass an added HTTP header entry for the username and password (encoded in base64), which can be done with a couple of the WebBrowser.Navigate overrides. For example:

String base64 = Convert.ToBase64String(ASCIIEncoding.
  ASCII.GetBytes("username:password"));
String auth = String.Format(
  "Authorization: Basic {0}\r\n",
  base64);
byte[] post = ASCIIEncoding.ASCII.GetBytes(
  auth.ToCharArray());
String targetFrameName = "";
byte[] postData = null;
Uri uri = new Uri("http://www.contoso.com/");
webBrowser1.Navigate(uri, targetFrameName, postData, auth);

No TrackBacks

TrackBack URL: http://www.iwebthereforeiam.com/cgi-bin/mt/mt-tb.cgi/574

Leave a comment

Verification (needed to reduce spam):

Pages

OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.32-en

About this Entry

This page contains a single entry by Hugh Brown published on March 12, 2006 5:33 AM.

Cramer's 25 Rules for Investing was the previous entry in this blog.

Fire and motion is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.