【问题标题】:How to open an url with basic authorization with default browser?如何使用默认浏览器打开具有基本授权的网址?
【发布时间】:2014-06-16 08:25:05
【问题描述】:

我需要使用用户凭据在 C# 程序中打开一个 URL。我尝试像这样使用 Winform WebBrowser

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

而且效果很好。但是,winform WebBrowser 窗口不像 IE、Fireworks、Chrome 等浏览器那样用户友好。

所以我想知道有什么方法可以在默认浏览器中以基本授权打开 URL。寻找任何想法。谢谢!!!

【问题讨论】:

  • 我搜索了很长一段时间,我发现的唯一可能的解决方案是这样的:http(s)://username:password@example.com/userProfile 但它暴露了凭据......

标签: c# url authentication


【解决方案1】:
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)
    {
    }
}

你是这个意思吗?

【讨论】:

  • 感谢您的回答!实际上,我想做的是在使用 IE 等浏览器打开网站时传递凭据信息。这可以通过使用 WebBrowser 来完成。但是,它对用户不友好。
【解决方案2】:

我回答了类似的问题here on stackoverflow...

回顾: .Net 中的 WebBrowser 控件使用 Internet Explorer 作为浏览器,所以如果您不介意使用 IE,这是我编写的代码。 h5url 是您要在窗口中打开的 url。我的程序甚至没有显示浏览器控件,它生成了一个 Internet Explorer 实例,其中登录的网页使用标头中编码的凭据信息。 (用户/密码变量)

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

            }

这将打开一个新浏览器,其中包含您进行身份验证所需的任何标头,无论是基本的还是其他的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 2014-12-06
    • 2014-03-11
    • 1970-01-01
    • 2012-02-04
    • 1970-01-01
    相关资源
    最近更新 更多