2013-07-31 115 views
0

我的目标是改变证书友好的名字,因为我必须通过唯一的别名外部aplication ......我写了这个:如何设置证书友好名称

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

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

我们假设证书是一个X509Certificate2对象。

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