Ver Mensaje Individual
  #4 (permalink)  
Antiguo 12/03/2009, 09:25
Avatar de joselowolf
joselowolf
 
Fecha de Ingreso: octubre-2008
Mensajes: 22
Antigüedad: 15 años, 6 meses
Puntos: 1
De acuerdo Respuesta: Importar un cerficado mediante codigo en c#

Hola Yo..... Al fin lo encontreeeeeeeeeeee..... se hace asi.


Código:
class Program
	{
		/// <summary>
		/// The main entry point.
		/// </summary>
		[STAThread]
		static void Main( string[] args )
		{
		        try
                {
                    X509Store serviceRuntimeUserCertificateStore = new X509Store(StoreName.Root);
                    string baseDir = AppDomain.CurrentDomain.BaseDirectory;
                    string certificateFolder = "c:\\";
                    string certPath = Path.Combine(baseDir, certificateFolder);

                    string certificateFile = "user-calist.cer";
                    string certificatePassword = "somePassword";
                    string certificateLocation = certPath +  certificateFile;

                    InstallCertificate(certificateLocation, certificatePassword);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            		
		}

        private static void InstallCertificate(string certificatePath, string certificatePassword)
        {
            try
            {
                var serviceRuntimeUserCertificateStore = new X509Store(StoreName.Root);
                serviceRuntimeUserCertificateStore.Open(OpenFlags.ReadWrite);

                X509Certificate2 cert= null;

                try
                {
                    cert = new X509Certificate2(certificatePath, certificatePassword);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed to load certificate " + certificatePath);
                    //throw new DataException("Certificate appeared to load successfully but also seems to be null.", ex);
                }

                serviceRuntimeUserCertificateStore.Add(cert);
                serviceRuntimeUserCertificateStore.Close();
            }
            catch (Exception)
            {
                Console.WriteLine("Failed to install {0}.  Check the certificate index entry and verify the certificate file exists.", certificatePath);
            }
        }

	}
no olvidarse del los

using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;