2009-09-06 63 views
6

Tôi đang cố gắng cài đặt chứng chỉ trong Cửa hàng máy cục bộ trong hành động tùy chỉnh. Giấy chứng nhận đã được cài đặt, nhưng khi tôi sử dụng nó để truy vấn AWS, tôi nhận được lỗi này:Cài đặt chứng chỉ trong Tác vụ tùy chỉnh .MSI không hoạt động chính xác

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

Trình cài đặt đang chạy nâng, mục tiêu là Windows Vista.

Nếu tôi sử dụng một .exe riêng biệt để cài đặt cùng một chứng chỉ, sử dụng cùng mã chính xác, nó hoạt động. Vì vậy, nó khác gì khi cài đặt chứng chỉ bằng Trình cài đặt Windows?

Mã:

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"); 
    } 
} 

Trả lời

5

Vâng, ít nhất câu hỏi này mang lại cho tôi một huy hiệu sụt giảm cỏ dại ...

Nó bật ra được các điều khoản trên vào file key cài đặt. Tôi phải cấp cho tất cả người dùng quyền đọc.

Và đây là đoạn code tôi sử dụng để cấp tất cả (địa phương) người dùng đọc các điều khoản:

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"; 
} 
Các vấn đề liên quan