Opc.Ua.WindowsCertificateStore.Add C# (CSharp) Method

Add() public method

public Add ( X509Certificate2 certificate ) : void
certificate System.Security.Cryptography.X509Certificates.X509Certificate2
return void
        public void Add(X509Certificate2 certificate)
        {   
            if (certificate == null) throw new ArgumentNullException("certificate");
         
            lock (m_lock)
            {
                IntPtr hStore = IntPtr.Zero;
                IntPtr pCertContext = IntPtr.Zero;   
                
	            // get the DER encoded data.
	            byte[] buffer = certificate.GetRawCertData();
                IntPtr pData = Copy(buffer);

	            // find the certificate.
	            try
                {
                    // open store.
                    hStore = OpenStore(false, true, true);

                    // check for existing certificate.
                    pCertContext = FindCertificate(hStore, certificate.Thumbprint);

                    if (pCertContext != IntPtr.Zero)
                    {
                        throw ServiceResultException.Create(
                            StatusCodes.BadUnexpectedError,
                            "Certificate is already in the store.\r\nType={0}, Name={1}, Subject={2}",
                            m_storeType,
                            m_symbolicName,
                            certificate.Subject);
                    }

                    // add certificate.
                    Opc.Ua.CertificateFactory.AddCertificateToWindowsStore(
                        m_storeType == WindowsStoreType.LocalMachine, 
                        m_symbolicName, 
                        certificate);
	            }
	            finally
	            {
                    if (pData != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(pData);
                    }

                    if (pCertContext != IntPtr.Zero)
                    {
                        NativeMethods.CertFreeCertificateContext(pCertContext);
                    }

                    if (hStore != IntPtr.Zero)
                    {
                        NativeMethods.CertCloseStore(hStore, 0);
                    }
	            }
            }
        }