System.Net.CertificateValidationPal.GetRemoteCertificate C# (CSharp) Method

GetRemoteCertificate() static private method

static private GetRemoteCertificate ( SafeDeleteContext securityContext, X509Certificate2Collection &remoteCertificateCollection ) : X509Certificate2
securityContext SafeDeleteContext
remoteCertificateCollection System.Security.Cryptography.X509Certificates.X509Certificate2Collection
return System.Security.Cryptography.X509Certificates.X509Certificate2
        internal static X509Certificate2 GetRemoteCertificate(SafeDeleteContext securityContext, out X509Certificate2Collection remoteCertificateCollection)
        {
            remoteCertificateCollection = null;

            if (securityContext == null)
            {
                return null;
            }

            if (NetEventSource.IsEnabled) NetEventSource.Enter(securityContext);

            X509Certificate2 result = null;
            SafeFreeCertContext remoteContext = null;
            try
            {
                remoteContext = SSPIWrapper.QueryContextAttributes(GlobalSSPI.SSPISecureChannel, securityContext, Interop.SspiCli.ContextAttribute.SECPKG_ATTR_REMOTE_CERT_CONTEXT) as SafeFreeCertContext;
                if (remoteContext != null && !remoteContext.IsInvalid)
                {
                    result = new X509Certificate2(remoteContext.DangerousGetHandle());
                }
            }
            finally
            {
                if (remoteContext != null && !remoteContext.IsInvalid)
                {
                    remoteCertificateCollection = UnmanagedCertificateContext.GetRemoteCertificatesFromStoreContext(remoteContext);

                    remoteContext.Dispose();
                }
            }

            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Log.RemoteCertificate(result);
                NetEventSource.Exit(null, result, securityContext);
            }
            return result;
        }