如何设置证书友好名称

时间:2013-07-31 05:33:04

标签: c# x509certificate

我的目标是更改证书友好名称,因为我必须将唯一别名传递给外部应用程序...我写道:

private void button1_Click(object sender, RoutedEventArgs e)
{
   X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
   store.Open(OpenFlags.ReadWrite | OpenFlags.OpenExistingOnly);
   X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
   X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
   X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(fcollection, "Test Certificate Select", "Select a certificate from the following list to get information on that certificate", X509SelectionFlag.MultiSelection);

   foreach (X509Certificate2 x509 in scollection)
        {
            try
            {
                String originName = friendlyNameOrigin(x509);  //get orginal friendlyname
                String nameNormalized = GetHashString(normalize(friendlyNameMy(x509))); //create 'uniqualy' name as md5 code
                MessageBox.Show(x509.FriendlyName);  //showed orginal name
                x509.FriendlyName = nameNormalized;
                MessageBox.Show(x509.FriendlyName);  //showed new name
                x509.Reset();
                //next line  - I started external aplication but it still see orginal name
                //var processInfo = new ProcessStartInfo("java.exe", "-jar dist3/Signer.jar \"" + nameNormalized + "\" " + path);
                //DO SOMETHING
                //x509.FriendlyName = originName;
            }
            catch (CryptographicException)
            {
                MessageBox.Show("Information could not be written out for this certificate.");
            }
        }
        store.Close();
}

此代码更改友好名称,但Windows操作系统仍然看到旧名称(程序运行时和此后)。这意味着设置友好名称不会更改证书的友好名称。

你能告诉我如何更改证书的友好名称吗?

感谢您的回答。

1 个答案:

答案 0 :(得分:1)

解决方案是使用商店。示例代码如下:

我们假设Certificate是X509Certificate2对象。

Certificate.FriendlyName = "New Friendly Name";
var store = new X509Store("My");
store.Open(OpenFlags.ReadWrite);
store.Add(Certificate);