MicrosoftEdge / MicrosoftEdge/WebView2Feedback

Unable to cast to Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Cookie

Open
#1,976 18 comments 0 reactions 1 assignee View on GitHub

@champnic is already working on this.

Since Dec 1, 2021.

bug priority-low tracked
Dominant language
PowerShell
Stars
526
Forks
67
PR merge metrics
No merged PRs in 30d

Description

I am using Visual Studio 2019 with a VB.Net application and DLLs that target .NET Framework 4.8
These components use the WebBrowser control and I'm now trying to convert it to use the WebView2 control
I have installed the Canary Channel MicrosoftEdgeSetupCanary.exe file version 1.3.153.53 and I'm using NuGet WebView2 version 1.0.1054.31
Currently I'm trying to use WebView21.CoreWebView2.CookieManager.GetCookiesAsync(m_loginHandler.BaseUrlPath) to retrieve the cookies that the WebBrowser control used to give me.
The List(Of CoreWebView2Cookie) returned has 1 item in it but when I try and access the cookie name & value I get

System.NotImplementedException
  HResult=0x80004001
  Message=Unable to cast to Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Cookie.
This may happen if you are using an interface not supported by the version of the WebView2 Runtime you are using.
For instance, if you are using an experimental interface from an older SDK that has been modified or removed in a newer runtime.
Or, if you are using a public interface from a newer SDK that wasn't implemented in an older runtime.
For more information about WebView2 versioning please visit the following: https://docs.microsoft.com/microsoft-edge/webview2/concepts/versioning
  Source=Microsoft.Web.WebView2.Core
  StackTrace:
   at Microsoft.Web.WebView2.Core.CoreWebView2Cookie.get__nativeICoreWebView2Cookie()
   at Microsoft.Web.WebView2.Core.CoreWebView2Cookie.get_Name()
   at Thunderhead.Web.LoginHandler.UpdateCookies(CookieCollection httpCookies) in C:\source.net\WebView\VB.NET\ThWeb\Login\LoginHandler.vb:line 237

I'm trying the following approach to get the html source and the cookies; the html source comes back perfectly.

        Private WithEvents m_htmlSourceTimer As Timers.Timer
        Private m_htmlSourceTask As Threading.Tasks.Task(Of String)
        Private m_htmlSource As String

        Private WithEvents m_getCookiesTimer As Timers.Timer
        Private m_getCookiesTask As Threading.Tasks.Task(Of List(Of CoreWebView2Cookie))

        Private WithEvents m_metaDataCheckTimer As Timers.Timer

        Private Sub NavigationCompleted(ByVal sender As Object, ByVal e As EventArgs)
            EnableTimers()
        End Sub
		
        Private Sub EnableTimers()
            If m_domContentedLoaded Then
                If m_htmlSourceTimer Is Nothing Then
                    m_htmlSourceTimer = NewTimer()
                End If
                If Not m_htmlSourceTimer.Enabled Then
                    m_htmlSourceTask = WebView21.CoreWebView2.ExecuteScriptAsync("document.documentElement.outerHTML;")
                    m_htmlSourceTimer.Start()
                End If
                If m_getCookiesTimer Is Nothing Then
                    m_getCookiesTimer = NewTimer()
                End If
                If Not m_getCookiesTimer.Enabled Then
                    m_getCookiesTask = WebView21.CoreWebView2.CookieManager.GetCookiesAsync(m_loginHandler.BaseUrlPath)
                    m_getCookiesTimer.Start()
                End If
            End If
        End Sub

        Private Function NewTimer() As Timers.Timer
            Dim result As New Timers.Timer
            result.Interval = 250
            result.AutoReset = True
            Return result
        End Function

        Private Sub CookieAndHtmlSourceTimerElapsed(ByVal sender As Object, ByVal e As EventArgs) Handles m_htmlSourceTimer.Elapsed, m_getCookiesTimer.Elapsed
            If m_htmlSourceTask.IsCompleted Then
                m_htmlSourceTimer.Stop()
                m_htmlSource = WebView2Helper.WebViewHTML(m_htmlSourceTask.Result)
            End If
            If m_getCookiesTask.IsCompleted Then
                m_getCookiesTimer.Stop()
                m_loginHandler.WebView2Cookies = m_getCookiesTask.Result
            End If
            If m_htmlSourceTask.IsCompleted AndAlso m_getCookiesTask.IsCompleted Then
                If m_metaDataCheckTimer Is Nothing Then
                    m_metaDataCheckTimer = NewTimer()
                End If
                If Not m_metaDataCheckTimer.Enabled Then
                    m_metaDataCheckTimer.Start()
                End If
            End If
        End Sub

        Private Sub MetaDataCheckTimerElapsed(ByVal sender As Object, ByVal e As EventArgs) Handles m_metaDataCheckTimer.Elapsed
            m_metaDataCheckTimer.Stop()
            CheckForSalesforceLogin()
            CheckForToken()
        End Sub

I have a couple of questions

  1. Should I be using WebView21.CoreWebView2.CookieManager.GetCookiesAsync?
  2. If so, what am I doing wrong?

AB#37255788

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.