Forum Discussion

myMedia's avatar
myMedia
Partner
3 days ago

VB.Net WebBrowser -> WebView2 Solution

Following on from and acknowledging Steve PP's work on this... here is my VB version of his C# code that was posted recently. 

In short, if you've recently experienced issues getting Auth Codes, you may need to move to the new from the old Forms.WebBrowser control to the new Microsoft.Web.WebView2.WinForms.WebView2 

 

Original Thread for more info.... 

------------------------------------------------------------------------------------------------------------------

NEW MODULE FOR WebView2

------------------------------------------------------------------------------------------------------------------

Imports Microsoft.Web.WebView2.Core

 

Module OAuthLogin

 

Private Const csOAuthServer As String = "https://secure.myob.com/oauth2/account/authorize/"
Private Const csOAuthLogoff As String = "https://secure.myob.com/oauth2/account/LogOff/"
Private Const csOAuthScope As String = "CompanyFile"

Private MyCurrentURI As String = ""
Private MyUrl As String = ""
Private MyAuthCode As String = ""

Private WithEvents webVW2 As New Microsoft.Web.WebView2.WinForms.WebView2

 

Public Function GetAuthorizationCode_WEB2(config As IApiConfiguration) As String
        Try
            MyUrl = String.Format("{0}?client_id={1}&redirect_uri={2}&scope={3}&response_type=code", csOAuthServer, config.ClientId, HttpUtility.UrlEncode(config.RedirectUrl), csOAuthScope)
            MyCurrentURI = ""

            Dim frm As Form = New Form()
            frm.ShowIcon = False

            webVW2 = New Microsoft.Web.WebView2.WinForms.WebView2
            webVW2.Dock = DockStyle.Fill
            frm.Controls.Add(webVW2)

            webVW2.Source = New Uri(MyUrl)

            frm.Size = New Size(800, 600)
            frm.ShowDialog()

            Return MyAuthCode

        Catch ex As Exception
            Return MyAuthCode
        End Try
    End Function


    Private Function ExtractAuthorizationCode(ByVal uri As String) As String
        Try
            Dim uriObj As New Uri(uri)
            Dim queryParams = HttpUtility.ParseQueryString(uriObj.Query)
            Return queryParams("code")
        Catch ex As Exception
            Return ""
        End Try
    End Function

 

Private Sub webVW2_CoreWebView2InitializationCompleted(sender As Object, e As CoreWebView2InitializationCompletedEventArgs) Handles webVW2.CoreWebView2InitializationCompleted
        Try
            If e.IsSuccess Then
                webVW2.CoreWebView2.Navigate(MyUrl)
            End If
        Catch ex As Exception
        End Try
    End Sub

 

Private Sub webVW2_NavigationStarting(sender As Object, e As CoreWebView2NavigationStartingEventArgs) Handles webVW2.NavigationStarting
        Try
            MyCurrentURI = e.Uri
        Catch ex As Exception
        End Try
    End Sub

 

Private Sub webVW2_NavigationCompleted(sender As Object, e As CoreWebView2NavigationCompletedEventArgs) Handles webVW2.NavigationCompleted
        Try
            If MyCurrentURI.Contains("code=") Then
                MyAuthCode = ExtractAuthorizationCode(MyCurrentURI)
            End If
        Catch ex As Exception
        End Try
    End Sub

 

End Module

No RepliesBe the first to reply