1

Implementing Certificates with CertEnroll dll on an Asp.net fw 4 website:

    CX509CertificateRequestPkcs10 objPkcs10 = new CX509CertificateRequestPkcs10Class();
    CX509PrivateKey objPrivateKey = new CX509PrivateKeyClass();
    CCspInformation objCSP = new CCspInformationClass();
    CCspInformations objCSPs = new CCspInformationsClass();
    CX500DistinguishedName objDN = new CX500DistinguishedNameClass();
    CX509Enrollment objEnroll = new CX509EnrollmentClass();
    CObjectIds objObjectIds = new CObjectIdsClass();
    CObjectId objObjectId = new CObjectIdClass();
    CX509ExtensionKeyUsage objExtensionKeyUsage = new CX509ExtensionKeyUsageClass();
    CX509ExtensionEnhancedKeyUsage objX509ExtensionEnhancedKeyUsage = new CX509ExtensionEnhancedKeyUsageClass();
    string strRequest;


    try
    {

        //  Initialize the csp object using the desired Cryptograhic Service Provider (CSP)
        objCSP.InitializeFromName(
            "Microsoft Enhanced Cryptographic Provider v1.0"
        );
        //  Add this CSP object to the CSP collection object
        objCSPs.Add(
            objCSP
        );
        //  Provide key container name, key length and key spec to the private key object
        //objPrivateKey.ContainerName = "AlejaCMa";
        objPrivateKey.Length = 1024;
        objPrivateKey.KeySpec = X509KeySpec.XCN_AT_SIGNATURE;
        objPrivateKey.KeyUsage = X509PrivateKeyUsageFlags.XCN_NCRYPT_ALLOW_ALL_USAGES;
        objPrivateKey.MachineContext = false;


        //  Provide the CSP collection object (in this case containing only 1 CSP object)
        //  to the private key object
        objPrivateKey.CspInformations = objCSPs;

        //  Create the actual key pair
        objPrivateKey.Create();
......

It is throwing the following error message (on the last line)

Certificate Error CertEnroll::CX509PrivateKey::Create: The system cannot find the file specified. 0x80070002 (WIN32: 2 ERROR_FILE_NOT_FOUND) 

When debugging it passes the instruction Create() without errors... but on my test environment: Windows Server 2012 it is failing

source: http://blogs.msdn.com/b/alejacma/archive/2008/09/05/how-to-create-a-certificate-request-with-certenroll-and-net-c.aspx

Osvier
  • 810
  • 1
  • 8
  • 24
  • Can you test this on another environment to see if the problem is with the environment or something else? – Yacoub Massad Nov 07 '15 at 01:50
  • I am having the same problem on my dev environment.. :/ – Osvier Nov 07 '15 at 16:39
  • Related posts - For on-premise deployement : [CryptographicException was unhandled: System cannot find the specified file](https://stackoverflow.com/q/17840825/465053). For Azure deployment : [Certenroll on Azure - Generating self signed certificates](https://stackoverflow.com/q/58800004/465053) – RBT Nov 21 '21 at 08:03

1 Answers1

1

I fixed this by setting Load User Profile = True in IIS advanced settings

See Tim Erikson answer here

Community
  • 1
  • 1
Osvier
  • 810
  • 1
  • 8
  • 24