16

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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Erik Sapir
  • 23,209
  • 28
  • 81
  • 141
  • 2
    I am trying do. There are many questions without good response – Erik Sapir May 18 '11 at 10:02
  • okay, so coming to your question, you want to pass the authentication details in the URL? – painotpi May 18 '11 at 10:04
  • 1
    @Erick Sapir: you can't control the headers a typical browser sends. If you embed a web browser control you may be able to access the headers sent but this does mean you have to distribute your special browser. My answer includes the historical method for embedding credentials in the URL and having these passed as a header. – Brian Lyttle May 18 '11 at 19:00
  • afaik, there is no way to do that. – abatishchev May 18 '11 at 10:53
  • If you don't mind using IE from the default WebBrowser control, you can see my answer below, pass the headers and tell it to open in a new window. – Billy Willoughby Sep 24 '17 at 16:49

5 Answers5

14

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:ppppppp@example.com", null, null, authHdr);
Alex K.
  • 171,639
  • 30
  • 264
  • 288
6

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.

Brian Lyttle
  • 14,558
  • 15
  • 68
  • 104
2

Try to use something like Watin Here you can find good blog-posts about Watin.

The code looks like:

public void SearchForWatiNOnGoogle()
{
  using (var browser = new IE("http://www.google.com"))
  {
    browser.TextField(Find.ByName("q")).TypeText("WatiN");
    browser.Button(Find.ByName("btnG")).Click();
  }
}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
oxilumin
  • 4,775
  • 2
  • 18
  • 25
0

The WebBrowser control in .Net uses Internet Explorer as it's browser, so if you don't mind using IE, this is the code I wrote. h5url is the url you want to open in a window. My program doesn't even show a browser control, this is spawns an instance of Internet Explorer with the web page logged in.

     using (WebBrowser WebBrowser1 = new WebBrowser())
            {
                String auth =
                    System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(_User + ":" + _Password));
                string headers = "Authorization: Basic " + auth + "\r\n";
                WebBrowser1.Navigate(h5URL, "_blank", null, headers);

            }

This opens a new browser with any headers you need for authentication, basic or otherwise.

Billy Willoughby
  • 806
  • 11
  • 15
0

First check this code:

Dim result As String

Using wClnt As New Net.WebClient

    wClnt.Credentials = New System.Net.NetworkCredential("username", "password")

    Using strR As New IO.StreamReader(wClnt.OpenRead("http://ADDRESS_To_READ"))

        result = strR.ReadToEnd

    End Using

End Using

If it was not what your where looking for, Check this post, it may help:

How do I log into a site with WebClient?

Update:

This way you are not opening any browser. Just requesting the address you want and passing Credential.

Community
  • 1
  • 1
Afshin Gh
  • 7,918
  • 2
  • 26
  • 43