0

I try to insert a authentification in webview2, but I have a error in my code.

The error is at the AddHandler WebAccount.CoreWebView2.BasicAuthenticationRequested :

System.NullReferenceException : 'Object reference not set to an instance of an object.'

Here my code:

Class MainWindow
Private Site As String = "https://test.fr"
Private HTTPS_LoginOne As String = "admin"
Private HTTPS_PwdOne As String = "1234567"
Public Event BasicAuthenticationRequested As EventHandler(Of CoreWebView2BasicAuthenticationRequestedEventArgs)

Public Sub New()

    InitializeComponent()

    InitWeb()

    AddHandler WebAccount.CoreWebView2.BasicAuthenticationRequested, AddressOf Authentweb

End Sub

Private Sub Authentweb(sender As Object, e As CoreWebView2BasicAuthenticationRequestedEventArgs)
    e.Response.UserName = HTTPS_LoginOne
    e.Response.Password = HTTPS_PwdOne
End Sub

Async Sub InitWeb()
    Await WebAccount.EnsureCoreWebView2Async()
    WebAccount.CoreWebView2.Navigate(Site)
End Sub

Someone have any idea ?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • `InitWeb()` is not awaited. Move the code in that method to the `Loaded` / `Load` event handler, make the handler `async` and await there `WebAccount.EnsureCoreWebView2Async()`. Subscribe to `BasicAuthenticationRequested` after -- Event handlers can be `async void` (`async sub`), other methods, not so much – Jimi Dec 30 '22 at 23:32
  • Thanks a lot . I no longer have the error. But I don't have the request for the credentials. – François HEBERT Dec 31 '22 at 00:00
  • Did you add the `BasicAuthenticationRequested` handler after `Await WebAccount.EnsureCoreWebView2Async()` and before `.Navigate()`? – Jimi Dec 31 '22 at 00:16
  • Yes I added like that – François HEBERT Dec 31 '22 at 00:25
  • Well, I cannot test your connection or EndPoint, I don't know whether the Service you're connecting to uses basic auth, NTLM challenge or you're trying to authenticate to a Proxy in between. You'll have to investigate that further – Jimi Dec 31 '22 at 00:28
  • The connection use htaccess – François HEBERT Dec 31 '22 at 00:42
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ken White Dec 31 '22 at 01:08
  • The following may be helpful: https://stackoverflow.com/a/73846538/10024425 and [Basic authentication for WebView2 apps](https://learn.microsoft.com/en-us/microsoft-edge/webview2/concepts/basic-authentication?tabs=csharp) – Tu deschizi eu inchid Dec 31 '22 at 02:30
  • @KenWhite, Yes I have my answer. And I fount why I didn't have the authentification request. I was in debug mode. In mode release, I have the request. But I don't know why . – François HEBERT Dec 31 '22 at 13:55
  • Most likely due to improper CoreWebView2 initialization. Delete the `UserDataFolder ` prior to execution to verify. – Tu deschizi eu inchid Dec 31 '22 at 14:58

0 Answers0