CefSharp icon indicating copy to clipboard operation
CefSharp copied to clipboard

Winforms - ISelectClientCertificateCallback.Select exception

Open chancelyg opened this issue 6 years ago • 8 comments

What version of the product are you using? built from 3700 CEF branch. Visual Studio 2015

What architecture x86 or x64? x86 build running on an x64 machine

On what operating system? Win10

Are you using WinForms, WPF or OffScreen? WinForms

What steps will reproduce the problem? Select a client certificate and then show exception

Please provide any additional information below. See document cefsharp.github.io - IRequestHandler.OnSelectClientCertificate Method

OnSelectClientCertificate
Return Value
Type: Boolean
Return true to continue the request and call ISelectClientCertificateCallback.Select() with the selected certificate for authentication. Return false to use the default behavior where the browser selects the first certificate from the list.

In the CefSharp.WinForms.Example project OnSelectClientCertificate function code is like this

protected override bool OnSelectClientCertificate(IWebBrowser chromiumWebBrowser, IBrowser browser, bool isProxy, string host, int port, X509Certificate2Collection certificates, ISelectClientCertificateCallback callback)
        {
            var control = (Control)chromiumWebBrowser;

            control.InvokeOnUiThreadIfRequired(delegate ()
            {
                var selectedCertificateCollection = X509Certificate2UI.SelectFromCollection(certificates, "Certificates Dialog", "Select Certificate for authentication", X509SelectionFlag.SingleSelection);

                //X509Certificate2UI.SelectFromCollection returns a collection, we've used SingleSelection, so just take the first
                //The underlying CEF implementation only accepts a single certificate
                callback.Select(selectedCertificateCollection[0]);
            });

            return true;
        }

Throw C++ exception by after in call "callback.Select(selectedCertificateCollection[0]);"

The program running normally if not call "InvokeOnUiThreadIfRequired" function and just like this

 protected override bool OnSelectClientCertificate(IWebBrowser chromiumWebBrowser, IBrowser browser, bool isProxy, string host, int port, X509Certificate2Collection certificates, ISelectClientCertificateCallback callback)
        {
            var control = (Control)chromiumWebBrowser;


            var selectedCertificateCollection = X509Certificate2UI.SelectFromCollection(certificates, "Certificates Dialog", "Select Certificate for authentication", X509SelectionFlag.SingleSelection);

            ////X509Certificate2UI.SelectFromCollection returns a collection, we've used SingleSelection, so just take the first
            ////The underlying CEF implementation only accepts a single certificate
            callback.Select(selectedCertificateCollection[0]);

            return true;
        }

Below is stack info

'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\14.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\14.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\14.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\Users\chancel\Desktop\CefSharp-master\CefSharp.WinForms.Example\bin\x86\Debug\CefSharp.WinForms.Example.vshost.exe'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Security\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Security.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\mscorlib.resources\v4.0_4.0.0.0_zh-Hans_b77a5c561934e089\mscorlib.resources.dll'. Module was built without symbols.
The thread 0x3384 has exited with code 0 (0x0).
The thread 0x36d0 has exited with code 0 (0x0).
The thread 0xd9c has exited with code 0 (0x0).
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\Users\chancel\Desktop\CefSharp-master\CefSharp.WinForms.Example\bin\x86\Debug\CefSharp.WinForms.Example.exe'. Symbols loaded.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\Users\chancel\Desktop\CefSharp-master\CefSharp.WinForms.Example\bin\x86\Debug\CefSharp.Core.dll'. Symbols loaded.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\Users\chancel\Desktop\CefSharp-master\CefSharp.WinForms.Example\bin\x86\Debug\CefSharp.Example.dll'. Symbols loaded.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\Users\chancel\Desktop\CefSharp-master\CefSharp.WinForms.Example\bin\x86\Debug\CefSharp.dll'. Symbols loaded.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\Users\chancel\Desktop\CefSharp-master\CefSharp.WinForms.Example\bin\x86\Debug\CefSharp.WinForms.dll'. Symbols loaded.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms.resources\v4.0_4.0.0.0_zh-Hans_b77a5c561934e089\System.Windows.Forms.resources.dll'. Module was built without symbols.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel\v4.0_4.0.0.0__b77a5c561934e089\System.ServiceModel.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\SMDiagnostics\v4.0_4.0.0.0__b77a5c561934e089\SMDiagnostics.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel.Internals\v4.0_4.0.0.0__31bf3856ad364e35\System.ServiceModel.Internals.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Transactions\v4.0_4.0.0.0__b77a5c561934e089\System.Transactions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.Serialization\v4.0_4.0.0.0__b77a5c561934e089\System.Runtime.Serialization.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.IdentityModel\v4.0_4.0.0.0__b77a5c561934e089\System.IdentityModel.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CefSharp.WinForms.Example.vshost.exe' (CLR v4.0.30319: CefSharp.WinForms.Example.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.VisualStudio.Diagnostics.ServiceModelSink\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Diagnostics.ServiceModelSink.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
CookieName: CefSharpTestCookie
CookieName: CefSharpTestCookie
CookieName: session_id
OnKeyEvent: KeyType: KeyUp 0x9 Modifiers: None
Exception thrown: 'System.AccessViolationException' in CefSharp.Core.dll

So I did not know why to call "InvokeOnUiThreadIfRequired" because the normal running just like do not need a call that function?

chancelyg avatar Oct 31 '19 07:10 chancelyg

This looks very much like a duplicate of #2853

built from 3700 CEF branch.

There is no such branch, please provide a link to the actual branch. The current supported branch is https://github.com/cefsharp/CefSharp/tree/cefsharp/75

In the CefSharp.WinForms.Example project OnSelectClientCertificate function code is like this

This is an outdated example. The example was updated in response to your previous issue https://github.com/cefsharp/CefSharp/issues/2853#issuecomment-516777817

Below is stack info

This isn't a stack trade, this looks more like the debugger console output. You'll need an actual stack trace.

Select a client certificate and then show exception

Please provide a more detailed list of steps to reproduce.

amaitland avatar Oct 31 '19 08:10 amaitland

This looks very much like a duplicate of #2853

Yes, I meet this question in #2853 and at that time I can't reappear this bug and so I copy #2853 question summary

There is no such branch, please provide a link to the actual branch. The currently supported branch is https://github.com/cefsharp/CefSharp/tree/cefsharp/75

Sorry, I clone the master branch Now I clone the cefsharp/75 branch, this bug still being

This isn't a stack trade, this looks more like the debugger console output. You'll need an actual stack trace.

I paster output info, sorry. Below is the correct stack info

 	ucrtbased.dll!7c83f2f6()	Unknown
 	[Frames below may be incorrect and/or missing, no symbols loaded for ucrtbased.dll]	
 	[External Code]	
>	CefSharp.Core.dll!std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<scoped_refptr<CefX509Certificate> > > >::*() Line 86	C++
 	CefSharp.Core.dll!CefSharp::Internals::CefCertificateCallbackWrapper::Select(System::Security::Cryptography::X509Certificates::X509Certificate2^ cert) Line 76	C++
 	CefSharp.WinForms.Example.exe!CefSharp.WinForms.Example.Handlers.WinFormsRequestHandler.OnSelectClientCertificate.AnonymousMethod__0() Line 50	C#
 	[External Code]	
 	CefSharp.WinForms.Example.exe!CefSharp.WinForms.Example.Program.Main(string[] args) Line 98	C#
 	[External Code]	

Please provide a more detailed list of steps to reproduce.

I use this browser access need a client certificate website Throw an exception in alter selecting a certificate and just like this screenshot Throw an exception in altering confirm a certificate

image

chancelyg avatar Oct 31 '19 09:10 chancelyg

Unfortunately this isn't an easy issue to debug, I don't have anything setup that requires certificate selection.

If you debug the source and step through the code you might get a better understanding of what's going on.

amaitland avatar Nov 08 '19 23:11 amaitland

Closing due to lack of feedback. If this reproduces with the current supported version then this can be reopened. See https://github.com/cefsharp/CefSharp/releases for current release details.

amaitland avatar Nov 10 '20 04:11 amaitland

Additional information at https://github.com/cefsharp/CefSharp/discussions/3334

amaitland avatar Dec 31 '20 22:12 amaitland

Based on https://github.com/cefsharp/CefSharp/discussions/3334

It sounds like the vector is being freed. We can possibly copy the vector to resolve this (assuming the CefRefPtr instances are valid).

I can make a change, I'd nedd some to test and confirm it works.

amaitland avatar Jan 09 '21 19:01 amaitland

I am available to test, I seem to be having this exact same error. If the candidate fix is simple, I'd greatly prefer you pointing me to what I should change.

Update: The solution from #3334 fixed my problem as well (comment out WinFormsRequestHandler, InvokeOnUiThreadIfRequired Line#42, so that the certificates load directly using the eventHandler thread).

thracx avatar Jan 13 '21 14:01 thracx

I am available to test, I seem to be having this exact same error. If the candidate fix is simple, I'd greatly prefer you pointing me to what I should change.

@thracx That would be very helpful. I should have a change ready for testing in the next few days. It's not exactly straight forward.

amaitland avatar Jan 21 '21 05:01 amaitland

There haven't been any reports since commit https://github.com/cefsharp/CefSharp/commit/a51cdd3723eb28cfd5a22cef06ab79e83413b905 was made. Hopefully the issue is resolved.

If the issue still reproduces in the current supported version then please comment here and the issue can be reopened.

amaitland avatar Dec 10 '23 01:12 amaitland