如何使用Authorization标头在.NET中启动URL?

时间:2014-12-05 02:20:33

标签: vb.net url http-authentication shellexecute

我们正在为客户创建Ellie Mae Encompass系统的插件。

这个特定的插件只有一个按钮,点击它时会启动一个URL并传入当前登录的Encompass用户的用户ID。使用以下代码非常简单......

Private Sub btnProductSearch_Click(sender As Object, e As EventArgs) 
Handles btnProductSearch.Click
    Try
        Dim cliid = EncompassApplication.Session.ClientID,
            unm As String = EncompassApplication.CurrentUser().ID,
            url As String = GetServiceUrl(cliid)

        If String.IsNullOrEmpty(url) Then
            MsgBox("No matching URL found for current Encompass Client ID.")
        Else
            Process.Start(url & unm)
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

现在有趣的部分......他们不希望用户必须登录正在启动的URL,但希望我们通过Authorization HTTP标头将身份验证传递给URL。

如果我们在代码中执行自己的Web请求,这很容易,如下所示:

    Dim wreq = HttpWebRequest.CreateHttp(url)
    wreq.Method = "POST"

    Dim authBytes As Byte() = Encoding.UTF8.GetBytes("EncodedInfoHere".ToCharArray())
    wreq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(authBytes))
    wreq.ContentType = "application/json"

但是,我们不会执行代码Web请求,而只是通过Process.Start启动URL。有没有办法在执行此操作时传递Authorization标题?

1 个答案:

答案 0 :(得分:2)

this question的帮助下,我在表单上删除了一个隐藏的WebBrowser控件,然后在外部浏览器中使用Authorization标题启动了URL,如下所示......

' wb is the WebBrowser 
Dim hdr = "Authorization: Basic " & Convert.ToBase64String(Encoding.UTF8.GetBytes("EncodedInfoHere".ToCharArray()))
wb.Navigate(url & unm, "_blank", {}, hdr)