3

I need to open an URL in a C# program with user credential. I tried to use Winform WebBrowser like this:

string user = "user";
string pass = "pass";
string authHdr = "Authorization: Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(user + ":" + pass)) + "\r\n";
webBrowser1.Navigate("http://example.com/userProfile", null, null, authHdr);

And it works fine. However, winform WebBrowser window is not as user friendly as browsers like IE, Fireworks, Chrome.

So I am wondering is there any way to open the URL in default browser with basic authorization. Look for any ideas. Thanks!!!

Sid
  • 31
  • 1
  • 4
  • 1
    I searched around for quite a while the only possible solution i found is like this: `http(s)://username:password@example.com/userProfile` but it exposes credentials.... – Sid May 01 '14 at 00:58

2 Answers2

0
using (System.Diagnostics.Process process = new System.Diagnostics.Process())
{
    try
    {
        process.StartInfo.FileName = "explorer.exe";
        process.StartInfo.Arguments = "http://stackoverflow.com/";
        process.Start();
    }
    catch (System.Exception e)
    {
    }
}

Do you mean this?

tim
  • 121
  • 4
  • Thanks for the answer! Actually, what i am trying to do is to pass credential information when open a website with browser like IE. This can be done by using WebBrowser. However, it is not user friendly. – Sid Apr 30 '14 at 23:28
0

I answered a similar question here on stackoverflow...

Recap: 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 with the credential information encoded in the header. (user / password variables)

     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