2009-09-06 4 views
6

मैं कस्टम मशीन में स्थानीय मशीन स्टोर में प्रमाणपत्र स्थापित करने का प्रयास कर रहा हूं। प्रमाण पत्र स्थापित किया गया है, लेकिन जब मैं इसका इस्तेमाल एडब्ल्यूएस क्वेरी करने के लिए, मैं इस त्रुटि मिलती है:.MSI कस्टम एक्शन में प्रमाणपत्र स्थापित करना ठीक से काम नहीं करता है

Object contains only the public half of a key pair. A private key must also be provided.

संस्थापक ऊंचा चल रहा है, लक्ष्य Windows Vista है।

यदि मैं सटीक उसी कोड का उपयोग करके सटीक उसी प्रमाणपत्र को स्थापित करने के लिए एक अलग .exe का उपयोग करता हूं, तो यह काम करता है। तो विंडोज इंस्टालर का उपयोग कर प्रमाण पत्र स्थापित करते समय यह क्या भिन्न है?

कोड:

private void InstallCertificate(string certificatePath, string certificatePassword) 
{ 
    if (IsAdmin()) 
    { 
    try 
    { 
     X509Certificate2 cert = new X509Certificate2(certificatePath, certificatePassword, 
     X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet); 

     X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine); 
     store.Open(OpenFlags.ReadWrite); 
     store.Add(cert); 
     store.Close(); 
    } 
    catch (Exception ex) 
    { 
     throw new DataException("Certificate appeared to load successfully but also seems to be null.", ex); 
    } 
    } 
    else 
    { 
    throw new Exception("Not enough priviliges to install certificate"); 
    } 
} 

उत्तर

5

ठीक है, कम से कम इस सवाल का मुझे एक लुढ़क घास बिल्ला ...

यह पता चला स्थापित कुंजी फ़ाइल पर अनुमतियों होने के लिए अर्जित किया। मुझे सभी उपयोगकर्ताओं को पढ़ने की अनुमति देना पड़ा।

private static void AddAccessToCertificate(X509Certificate2 cert) 
{ 
    RSACryptoServiceProvider rsa = cert.PrivateKey as RSACryptoServiceProvider; 
    if (rsa == null) return; 

    string keyfilepath = FindKeyLocation(rsa.CspKeyContainerInfo.UniqueKeyContainerName); 

    FileInfo file = new FileInfo(System.IO.Path.Combine(keyfilepath, rsa.CspKeyContainerInfo.UniqueKeyContainerName)); 

    FileSecurity fs = file.GetAccessControl(); 

    SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null); 
    fs.AddAccessRule(new FileSystemAccessRule(sid, FileSystemRights.Read, AccessControlType.Allow)); 
    file.SetAccessControl(fs); 
} 

private static string FindKeyLocation(string keyFileName) 
{ 
    string pathCommAppData = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), @"Microsoft\Crypto\RSA\MachineKeys"); 
    string[] textArray = Directory.GetFiles(pathCommAppData, keyFileName); 
    if (textArray.Length > 0) return pathCommAppData; 

    string pathAppData = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Microsoft\Crypto\RSA\"); 
    textArray = Directory.GetDirectories(pathAppData); 
    if (textArray.Length > 0) 
    { 
    foreach (string str in textArray) 
    { 
     textArray = Directory.GetFiles(str, keyFileName); 
     if (textArray.Length != 0) return str; 
    } 
    } 
    return "Private key exists but is not accessible"; 
} 
:

और यहाँ कोड मैं सभी (स्थानीय) उन अनुमतियों को पढ़ने देने के लिए प्रयोग किया जाता है

संबंधित मुद्दे