1.使用Capicom讀取數位憑證的方法
一、使用javascript方法:
<object id="oCAPICOM" codebase="http://download.microsoft.com/download/E/1/8/E18ED994-8005-4377-A7D7-0A8E13025B94/capicom.cab#version=2,0,0,3"classid="clsid:A996E48C-D3DC-4244-89F7-AFA33EC60679"></object>
<script language="javascript">
var CAPICOM_CURRENT_USER_STORE = 2
var CAPICOM_STORE_OPEN_READ_WRITE = 1
function auto_run() {
var CertSubject;
var CertSerial;
var oSignerCert;
var oSelects;
var oSignerCert;
var st = new ActiveXObject("CAPICOM.Store");
var Certificate = new ActiveXObject("CAPICOM.Certificate");
st.open(CAPICOM_CURRENT_USER_STORE, "my", CAPICOM_STORE_OPEN_READ_WRITE);
if (st.Certificates.Count == 1) {
oSignerCert = st.Certificates(1);
}
else {
oSelects = new ActiveXObject("CAPICOM.Certificates");
oSelects = st.Certificates.Select();
oSignerCert = oSelects(1);
}
document.all.text1.value = oSignerCert.Getinfo(6);
document.all.text2.value = oSignerCert.SerialNumber;
}
</script>
其他參考:http://www.yyable.com/zll/?p=37
http://blog.csdn.net/PKI_CA/article/details/2340261
二、使用Cryptography Objects:
詳見: http://msdn.microsoft.com/en-us/library/aa380254(v=VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms867087
SDK下載:http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=25281
注意MSDN的提示,原文如下:
Requirements
Redistributable
CAPICOM 2.0 or later on Windows Server 2003, Windows XP, Windows 2000 Server with SP3 and later, and Windows 2000 Professional with SP3 and later
DLL
-
Capicom.dll
2.實現讀取數位憑證序號功能時遇到的問題
一、解決瀏覽器彈出安全提示的問題:
如標題中所說,當使用JS調用Capicom時,IE會彈出安全提示。MSDN原文解釋如下:
Important When this method is called from a web script, the script needs to access digital certificates on the local computer. If you allow the script to access your digital certificates, the website from which the script is run will also gain access to any personal information stored in the certificates. The first time this method is called from a particular domain, a dialog box is generated in which the user must indicate whether access to the certificates should be allowed. Stores opened from a web script automatically force the CAPICOM_STORE_OPEN_EXISTING_ONLY flag.
我想到的一種解決辦法就是自己用C#寫一個ActiveX,然後再用指令碼調用這個ActiveX開放的方法來讀取數位憑證的資訊,這樣就跳過了瀏覽器的安全檢查。實際驗證,成功解決了IE彈出安全提示的問題。
我的這個ActiveX實現的功能是讀取USBKEY數位憑證序號及到期時間(使用SDK中的getChainFromStore方法獲得認證路勁找到指定的認證,然後讀取需要的資訊),代碼就不貼出來了。(項目需要引用Capicom這個COM組件,沒找到就用Regsvr32.exe Capicom.dll命令註冊)
關於怎麼用.NET開發Activex控制項可以看我的另一篇文章(含源碼下載):C#實現的ActiveX列印控制項
強調兩點:1.ActiveX項目屬性的“產生”項中“為COM Inerop 註冊”勾選之後本地調試會自動註冊該控制項(Windows7需要以管理員運行該項目才行)。可以使用“Microsoft Visual Studio 8\Common7\Tools\Bin”下的OleView.Exe查看是否註冊成功(以2005為例)。
2.安裝檔案項目中引用的ActiveX控制項項目或者其DLL檔案的屬性中,Register必須設定成“vsdrpCOM”。
二、解決C#開發的ActiveX控制項或者C++開發的.ocx副檔名的ActiveX在IE下不能成功安裝,需要降低瀏覽器許可權,或者根本裝不成功的問題
這個還是與IE的認證檢查有關,一種解決辦法就是製作ActiveX安裝檔案,而不是製作.dat包之類的。
C#開發ActiveX只要注意了我上面提到的屬性設定應該就不會有問題了。這裡用到了Capicom,還不得不寫成安裝檔案以註冊Capicom。
至於.ocx副檔名的ActiveX控制項,需要寫程式自動註冊,用到的命令是:Regsvr32.exe /s youfilename.ocx
這裡的/s達到的效果就是不彈出註冊成功與否的提示。
使用程式註冊.ocx副檔名的ActiveX控制項:
/// <summary>
/// 複製檔案到系統安裝路勁
/// </summary>
/// <param name="fileName">檔案名稱</param>
private void AddResourceFile(string fileName)
{
try
{
if (!File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\" + fileName))//不存在檔案則建立
{
Assembly asm = Assembly.GetExecutingAssembly();
using (Stream stream = asm.GetManifestResourceStream("InstallActiveX." + fileName))//檔案已經以“嵌入資源檔”的形式添加到項目中
{
int len = (int)stream.Length;
byte[] byts = new byte[len];
stream.Read(byts, 0, len);
stream.Close();
using (FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\" + fileName, FileMode.Create))
{
fs.Write(byts, 0, len);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "在" + ex.StackTrace);
}
}
class Register
{
/// <summary>
/// 使用regsvr註冊控制項
/// </summary>
/// <param name="strDll">檔案名稱</param>
public void RegisterDll(string strDll)
{
try
{
Process p = new Process();
p.StartInfo.FileName = "Regsvr32.exe";
p.StartInfo.Arguments = " " + strDll;
p.Start();
p.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "在" + ex.StackTrace);
}
}
}
使用:
AddResourceFile("youfilename.ocx");//先複製檔案
Register r = new Register();
r.RegisterDll("/s youfilename.ocx");//註冊ocx
關於怎麼讓這段程式在安裝時運行,方法是:建立一個安裝程式類,然後把該類所在的類庫添加到“自訂動作”的“安裝”檔案夾。
添加過程是,選擇安裝檔案項目,右鍵選擇“視圖”->“自訂動作”。
三、解決.Net Framework如何打包到安裝檔案的問題
設定中所示:
在安裝檔案項目的屬性中點擊“系統必備”設定。