CoreWebView2.ClientCertificateRequested Event

Definition

ClientCertificateRequested is raised when WebView2 is making a request to an HTTP server that needs a client certificate for HTTP authentication. Read more about HTTP client certificates at RFC 8446 The Transport Layer Security (TLS) Protocol Version 1.3.

C#
public event EventHandler<Microsoft.Web.WebView2.Core.CoreWebView2ClientCertificateRequestedEventArgs> ClientCertificateRequested;

Event Type

Examples

C#
// Turn off client certificate selection dialog using ClientCertificateRequested event handler
// that disables the dialog. This example hides the default client certificate dialog and
// always chooses the last certificate without prompting the user.
private bool _isCustomClientCertificateSelection = false;
void EnableCustomClientCertificateSelection()
{
    // Safeguarding the handler when unsupported runtime is used.
    try
    {
        if (!_isCustomClientCertificateSelection)
        {
            _iWebView2.CoreWebView2.ClientCertificateRequested += WebView_ClientCertificateRequested;
        }
        else
        {
            _iWebView2.CoreWebView2.ClientCertificateRequested -= WebView_ClientCertificateRequested;
        }
        _isCustomClientCertificateSelection = !_isCustomClientCertificateSelection;

        MessageBox.Show(this,
            _isCustomClientCertificateSelection ? "Custom client certificate selection has been enabled" : "Custom client certificate selection has been disabled",
            "Custom client certificate selection");
    }
    catch (NotImplementedException exception)
    {
        MessageBox.Show(this, "Custom client certificate selection Failed: " + exception.Message, "Custom client certificate selection");
    }
}

void WebView_ClientCertificateRequested(object sender, CoreWebView2ClientCertificateRequestedEventArgs e)
{
    IReadOnlyList<CoreWebView2ClientCertificate> certificateList = e.MutuallyTrustedCertificates;
    if (certificateList.Count() > 0)
    {
        // There is no significance to the order, picking a certificate arbitrarily.
        e.SelectedCertificate = certificateList.LastOrDefault();
        // Continue with the selected certificate to respond to the server.
        e.Handled = true;
    }
    else
    {
        // Continue without a certificate to respond to the server if certificate list is empty.
        e.Handled = true;
    }
}
C#
// This example hides the default client certificate dialog and shows a custom dialog instead.
// The dialog box displays mutually trusted certificates list and allows the user to select a certificate.
// Selecting `OK` will continue the request with a certificate.
// Selecting `CANCEL` will continue the request without a certificate
private bool _isCustomClientCertificateSelectionDialog = false;
void DeferredCustomClientCertificateSelectionDialog()
{
    // Safeguarding the handler when unsupported runtime is used.
    try
    {
        if (!_isCustomClientCertificateSelectionDialog)
        {
            _iWebView2.CoreWebView2.ClientCertificateRequested += delegate (
                object sender, CoreWebView2ClientCertificateRequestedEventArgs args)
            {
                // Developer can obtain a deferral for the event so that the WebView2
                // doesn't examine the properties we set on the event args until
                // after the deferral completes asynchronously.
                CoreWebView2Deferral deferral = args.GetDeferral();

                System.Threading.SynchronizationContext.Current.Post((_) =>
                {
                    using (deferral)
                    {
                        IReadOnlyList<CoreWebView2ClientCertificate> certificateList = args.MutuallyTrustedCertificates;
                        if (certificateList.Count() > 0)
                        {
                            // Display custom dialog box for the client certificate selection.
                            var dialog = new ClientCertificateSelectionDialog(
                                                        title: "Select a Certificate for authentication",
                                                        host: args.Host,
                                                        port: args.Port,
                                                        client_cert_list: certificateList);
                            if (dialog.ShowDialog() == true)
                            {
                                // Continue with the selected certificate to respond to the server if `OK` is selected.
                                args.SelectedCertificate = (CoreWebView2ClientCertificate)dialog.CertificateDataBinding.SelectedItem;
                            }
                            // Continue without a certificate to respond to the server if `CANCEL` is selected.
                            args.Handled = true;
                        }
                        else
                        {
                            // Continue without a certificate to respond to the server if certificate list is empty.
                            args.Handled = true;
                        }
                    }

                }, null);
            };
            _isCustomClientCertificateSelectionDialog = true;
            MessageBox.Show("Custom Client Certificate selection dialog will be used next when WebView2 is making a " +
                "request to an HTTP server that needs a client certificate.", "Client certificate selection");
        }
    }
    catch (NotImplementedException exception)
    {
        MessageBox.Show(this, "Custom client certificate selection dialog Failed: " + exception.Message, "Client certificate selection");
    }
}

Remarks

The host have several options for responding to client certificate requests:

ScenarioHandledCancelSelectedCertificate
Respond to server with a certificateTrueFalseMutuallyTrustedCertificate value
Respond to server without certificateTrueFalsenull
Display default client certificate selection dialog promptFalseFalsen/a
Cancel the requestn/aTruen/a

If the host don't handle the event, WebView2 will show the default client certificate selection dialog prompt to the user.

Applies to

Prodotto Versioni
WebView2 .NET 1.0.961.33, 1.0.992.28, 1.0.1020.30, 1.0.1054.31, 1.0.1072.54, 1.0.1108.44, 1.0.1150.38, 1.0.1185.39, 1.0.1210.39, 1.0.1245.22, 1.0.1264.42, 1.0.1293.44, 1.0.1343.22, 1.0.1370.28, 1.0.1418.22, 1.0.1462.37, 1.0.1518.46, 1.0.1587.40, 1.0.1661.34, 1.0.1722.45, 1.0.1774.30, 1.0.1823.32, 1.0.1901.177, 1.0.1938.49, 1.0.2045.28, 1.0.2088.41, 1.0.2151.40, 1.0.2210.55, 1.0.2277.86, 1.0.2365.46, 1.0.2420.47, 1.0.2478.35, 1.0.2535.41, 1.0.2592.51, 1.0.2651.64, 1.0.2739.15, 1.0.2792.45, 1.0.2849.39, 1.0.2903.40, 1.0.2957.106, 1.0.3065.39
WebView2 .NET Prerelease 1.0.902, 1.0.955, 1.0.1010, 1.0.1018, 1.0.1056, 1.0.1083, 1.0.1133, 1.0.1158, 1.0.1189, 1.0.1222, 1.0.1248, 1.0.1305, 1.0.1340, 1.0.1369, 1.0.1414, 1.0.1466, 1.0.1549, 1.0.1619, 1.0.1671, 1.0.1724, 1.0.1777, 1.0.1829, 1.0.1905, 1.0.1988, 1.0.2065, 1.0.2106, 1.0.2164, 1.0.2194, 1.0.2357, 1.0.2415, 1.0.2470, 1.0.2526, 1.0.2584, 1.0.2646, 1.0.2730, 1.0.2783, 1.0.2839, 1.0.2895, 1.0.2950, 1.0.3079, 1.0.3116