在webBrowser Forms Application中使用c#进行基本身份验证

时间:2016-06-06 10:08:05

标签: c# authentication basic-authentication explorer

我在网上搜索了这个问题,并发现这一行总是作为答案:

this.webBrowser1.Url = new System.Uri("https://username:password@url", System.UriKind.Absolute);

但IE不再支持此功能

现在我实现了这个:

this.webBrowser1.Url = new System.Uri("myurl", System.UriKind.Absolute);
this.webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);

在函数webBrowser1_DocumentCompleted中,我有以下内容:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
  string username = "myusername";
  string password = "mypassword";
  string auth = System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(username + ":" + password));
  string headers = "Authorization: Basic " + auth + "\r\n";
  webBrowser1.ScriptErrorsSuppressed = true;
  webBrowser1.Navigate("https://myurl", "", null, headers);
} // added for completeness by editor

这不会加载整个页面,并且每隔几秒就会刷新一次。有什么建议吗?

非常感谢。

0 个答案:

没有答案