Search code examples
vb.neturlhttp-authenticationshellexecute

How can I launch a URL in .NET with an Authorization header?


We are creating a plug-in to Ellie Mae's Encompass system for a client.

This particular plugin just has one button, which when clicked launches a URL and passes in the userid of the currently logged in Encompass user. This is all very straight forward using the following code...

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

Now for the fun part... they don't want the user to have to log in to the URL being launched, but want us to pass the authentication to the URL via an Authorization HTTP header.

This would be easy enough if we were doing our own web request in code, something like the following:

    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"

However, we are not doing a code web request, but rather are simply launching the URL via Process.Start. Is there any way while doing this to pass the Authorization header?


Solution

  • With help from this question, I dropped a hidden WebBrowser control on my form, and then launched the URL in an external browser, with Authorization header, as follows...

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