简体   繁体   中英

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

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?

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);

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.

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();
  }
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM