前言
最近同事詢問在 開發環境跑沒問題,一放到 Server 上程式就 GG 了。
將程式加上 log 來顯示,結果出現 System.Security.Cryptography.CryptographicException: The system cannot find the file specified. 的錯誤
System.Security.Cryptography.CryptographicException: The system cannot find the file specified.
at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
at System.Security.Cryptography.Utils.CreateCSP(CspParameters param, Boolean randomKeyContainer, SafeProvHandle& hProv)
at System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer)
at System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType keyType, CspParameters parameters, Boolean randomKeyContainer, Int32 dwKeySize, SafeProvHandle& safeProvHandle, SafeKeyHandle& safeKeyHandle)
at System.Security.Cryptography.RSACryptoServiceProvider.GetKeyPair()
at System.Security.Cryptography.RSACryptoServiceProvider..ctor(Int32 dwKeySize, CspParameters parameters, Boolean useDefaultKeySize)
at Org.BouncyCastle.Security.DotNetUtilities.CreateRSAProvider(RSAParameters rp) in //crypto/src/security/DotNetUtilities.cs:line 247
at Org.BouncyCastle.Security.DotNetUtilities.ToRSA(RsaKeyParameters rsaKey) in /_/crypto/src/security/DotNetUtilities.cs:line 158
解法
參考 CryptographicException was unhandled: System cannot find the specified file ,
因為 IIS 中的 Application Pool 進階設定中的 載入使用者設定檔(Load User Profile) 屬性預設是 false 。
所以程式中無法判別,所以只要將 載入使用者設定檔(Load User Profile) 屬性設定成 true 就可以了哦!
- 註 1: 也可以檢查一下
C:\Users\[app pool 識別帳號]\AppData\Roaming\Microsoft
目錄裡面有沒有 Crypto 的目錄 - 註 2: Crypto 的目錄中應該要有 RSA 目錄, RSA 目錄中會再包成 S-… 的目錄,裡面會有一個 GUID 的檔案
參考資料
CryptographicException was unhandled: System cannot find the specified file
System.Security.Cryptography.CryptographicException: The system cannot find the file specified.