Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i open a url in web browser (such as IE) and pass credentials

Tags:

c#

.net

.net-4.0

I want to open a page that required Basic authentication. I want to pass the Basic authentication header to the browser along with the URL.

How can i do that?

like image 240
Erik Sapir Avatar asked May 18 '11 09:05

Erik Sapir


2 Answers

Via a header you can:

string user = "uuuuuuu";
string pass = "ppppppp";
string authHdr = "Authorization: Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(user + ":" + pass)) + "\r\n";

webBrowserCtl.Navigate("http://example.com", null, null, authHdr);

given that this needs to be done on a per-request basis, an easier option for basic auth is to just;

webBrowserCtl.Navigate("http://uuuuuuu:[email protected]", null, null, authHdr);
like image 75
Alex K. Avatar answered Oct 11 '22 23:10

Alex K.


You could try the old "in URL" format which allowed this but it is insecure:

http(s)://username:password@server/resource.ext

This exposes credentials and IE has disabled it, but it may still work in other browsers. When this format is used the credentials are available to the browser and it makes the decision to send the basic authentication header depending on how the web server responds.

like image 6
Brian Lyttle Avatar answered Oct 12 '22 00:10

Brian Lyttle