2014-01-06 25 views
0

'
//使用的eToken類3鉑簽署文本如何使用asp.net webite的eToken在IIS中啓用域服務器

 static byte[] Sign(string text, string certSubject) 
    { 
     // Access Personal (MY) certificate store of current user 
     X509Store my = new X509Store(StoreName.My, StoreLocation.CurrentUser); 
     my.Open(OpenFlags.ReadOnly); 

     // Find the certificate we'll use to sign    
     RSACryptoServiceProvider csp = null; 
     foreach (X509Certificate2 cert in my.Certificates) 
     { 
      if (cert.Subject.Contains(certSubject)) 
      { 
       // We found it. 
       // Get its associated CSP and private key 
       csp = (RSACryptoServiceProvider)cert.PrivateKey; 
      } 
     } 
     if (**csp == null**) 
     { 
      throw new Exception("No valid cert was found"); 
     } 

     // Hash the data 
     SHA1Managed sha1 = new SHA1Managed(); 
     UnicodeEncoding encoding = new UnicodeEncoding(); 
     byte[] data = encoding.GetBytes(text); 
     byte[] hash = sha1.ComputeHash(data); 

     // Sign the hash 
     return csp.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"));    
    }` 

這codesnipt行之有效的Visual Studio,但回報CSP爲空,而在域服務器和本地IIS Server中使用此代碼。請告訴我什麼是我缺少的點。

回答

0

應用程序池標識無權訪問證書。

請注意,您正在訪問當前用戶的商店。這在VS中起作用,因爲你是當前用戶。但是,當您將應用程序部署到iis時,應用程序池標識最有可能不同。

建議 - 不要使用當前用戶的證書存儲區。原因是當您切換應用程序池的身份時,您必須登錄到每個帳戶並將證書導入到相應的證書存儲區。而是使用所有用戶共享的本地機器證書存儲。這裏存在一個小問題 - 必須將證書放置在本地機器商店的個人存儲中,並手動設置權限才能將私鑰存取到應用程序池的身份。

通過右鍵單擊證書,選擇「管理私鑰」,然後將應用程序池的標識添加到允許訪問私鑰的用戶列表中來完成此操作。

+0

您能否詳細說明將證書放入個人存儲並設置訪問私鑰的權限的過程。 – Chirag

0

當我將StoreLocation.CurrentUser更改爲StoreLocation.local機器時,它將主題顯示爲localhost,發行者也顯示爲localhost.this更改會生成相應的結果。我需要進行哪些更改,以便集成使用3級etoken的設施在我的網站上,這樣任何人都可以上傳或使用他們自己的3級認證登錄。

相關問題