Importing a PFX file using X509Certificate2 from ASP fails



Hi all!

I have a problem while importing a pfx file (or a pfx-byte-array) with X509Certificate2 constructor.
When I call

X509Certificate2 cert = new X509Certificate2("a.pfx", "password");

I get the following Exception:

+++++++++++++++++++++++++
System.Security.Cryptography.CryptographicException: The system cannot find the file specified
at System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr)
at System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromFile(String fileName, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle& pCertCtx)
at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromFile(String fileName, Object password, X509KeyStorageFlags keyStorageFlags)
at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(String fileName, String password)
+++++++++++++++++++++++++++

The problem occurs, because the asp.net application runs under a different account as "Networkservice", so this account hasn't a loaded userprofile.
The X509Certificate2-constructor couldn't access the Usercertificates- store.
This problem is also descriped in KB948154
(http://support.microsoft.com/kb/948154) But the descriped workaround in this article is not applicable for me.

Her the workaround-text:
############################
1. An administrator on the machine where the ASP.NET/COM+ application runs should install the certificate in the machine certificate store, called the "Local Computer" store. This should be done when the ASP.NET/COM+ application is installed.


2. The administrator should set the permissions on the private key associated with the certificate to give the ASP.NET process and the impersonated users access to the key. This is needed because only the user account that installs the certificate or private key in the "Local Computer" store can later use the RSA private key associated with the certificate.

3. The ASP.NET/COM+ application code should use the installed certificate rather than attempt to install one from a PFX file. Have the code locate the installed certificate using X509Store class.

a. Use StoreLocation.LocalMachine in the constructor of X509Store.
b. Once the store is opened, locate the desired certificate based on subject name programmatically using X509Certificate2Collection.Find method.

Example code will look similar to:

X509Store store = new X509Store("My", StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);

System.Security.Cryptography.X509Certificates.X509Certificate2 newCert =
store.Certificates.Find(X509FindType.FindBySubjectName, "XXXXXXXXXXXXX", false)[0];


The subject name to use above in the second parameter of Find method will depend on the subject name of the installed certificate from p12 or PFX file in the Local Computer certificate store.
##############################

Has anyone a similar problem an another solution?
Thanx for all posts!

Greetings
Pat
.



Relevant Pages

  • Re: Unable to unwrap a symmetric key using the private key of an X.509
    ... The problem is related to the certificate store on the web service side. ... You installed the certificate in "OtherPeople" store but the policy points ... You should install the certificate in the "Personal" store. ...
    (microsoft.public.dotnet.framework.webservices.enhancements)
  • Re: Install SSL Cert - NO KEY Manger in IIS 5
    ... Select Local Computer on the next screen, ... Click next, browse to the PFX file, select Next ... Choose ASSIGN a certificate already on the server...... ... take this server offline and install the the *.KEY file on ...
    (microsoft.public.inetserver.iis.security)
  • Re: Programmatically installing Client Root Certificates
    ... You install root certificates into the "root" store. ... the certificate to the root store. ...
    (microsoft.public.platformsdk.security)
  • Re: RPC over HTTP 1st time w/ Vista - Trouble installing Certificate
    ... And do NOT select automatic store - you need to put it in the Root store. ... The reason is that the elevated user is not the same as the regular user, and the default is to put it in the user's store. ... From waht i can tell everything is setup correctly, but the only thing that isn't going as planned is the installation of the certificate. ... Normally I have the user go to the mail.mycompany.com/exchange and install the certificate at the warning that pops up. ...
    (microsoft.public.windows.server.sbs)
  • Re: The security certificate on the server is invalid
    ... I managed to get the pfx file onto the 700w, but I have no idea how to ... You could just copy the cert to the 700w and install it there. ... Exchange 2003 SP2 on my SBS server. ... Windows Mobile lets you turn certificate checking off. ...
    (microsoft.public.pocketpc.activesync)