通过移除FiddlerCore安装的默认CertMaker.dll
和BcMakeCert.dll
程序集,并使用和分发makecert.exe
可执行文件,我能够解决这个问题并创建可在EXE会话间使用的持久证书。
makecert.exe
似乎以一种可用于应用程序多次运行的方式创建证书,而包含的程序集仅对当前应用程序的运行会话有效。
更新:
如果您想使用FiddlerCore默认安装的CertMaker.dll
和BcMakeCert.dll
,则必须使用Fiddler的内部首选项对象有效地缓存和设置证书和私钥。有几个键保存了证书被创建后的值,您需要捕获这些值,并将它们写入某种配置存储中。
在下面的例子中,我有一个静态配置对象,保存证书和密钥(在应用程序关闭时持久化到配置文件):
public static bool InstallCertificate()
{
if (!CertMaker.rootCertExists())
{
if (!CertMaker.createRootCert())
return false;
if (!CertMaker.trustRootCert())
return false;
App.Configuration.UrlCapture.Cert =
FiddlerApplication.Prefs.GetStringPref("fiddler.certmaker.bc.cert", null);
App.Configuration.UrlCapture.Key =
FiddlerApplication.Prefs.GetStringPref("fiddler.certmaker.bc.key", null);
}
return true;
}
public static bool UninstallCertificate()
{
if (CertMaker.rootCertExists())
{
if (!CertMaker.removeFiddlerGeneratedCerts(true))
return false;
}
App.Configuration.UrlCapture.Cert = null;
App.Configuration.UrlCapture.Key = null;
return true;
}
安装证书后,此代码会将证书和私钥捕获到配置对象中,并稍后保留该值。卸载时,这些值被清除。
在应用程序开始或捕获过程开始之前,在调用CertMaker.rootCertExists()
之前,从配置值设置密钥。我在我的捕获表单的开头执行此操作:
public FiddlerCapture()
{
InitializeComponent();
if (!string.IsNullOrEmpty(App.Configuration.UrlCapture.Cert))
{
FiddlerApplication.Prefs.SetStringPref("fiddler.certmaker.bc.key",
App.Configuration.UrlCapture.Key);
FiddlerApplication.Prefs.SetStringPref("fiddler.certmaker.bc.cert",
App.Configuration.UrlCapture.Cert);
}
}
当使用CertMaker.dll时,使用此机制保存和设置捕获设置可使证书在多个EXE会话中持久存在。
有关更详细的信息,请参阅FiddlerCore上的这篇详细博客文章。