4

When I encounter:

  View Certificate: .\SecurityTool.exe

System.Security.Cryptography.CryptographicException: Cannot find the requested object.

   at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
   at System.Security.Cryptography.X509Certificates.X509Utils._QueryCertFileType(String fileName)
   at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromFile(String fileName, Object password, X509KeyStorageFlags keyS
torageFlags)
   at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(String fileName)
   at CORSIS.PortFusion.Security.Tool.view@167-1.Invoke(String file) in C:\CORSIS\Software\PortFusion\0.9.3\PortFusionSource\PortFusion\SecurityTool\P
rogram.fs:line 170

   >  Private Key Password
   =  ""
   =  PS C:\CORSIS\Software\PortFusion\0.9.3\PortFusionSource\PortFusion\SecurityTool\bin\Debug> .\SecurityTool certificates view .\go.pfx
CORSIS PortFusion : Distributed Reverse Proxy Security Tool 0.9.8.0



  View Certificate: .\go.pfx

System.Security.Cryptography.CryptographicException: The specified network password is not correct.

   at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
   at System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromFile(String fileName, IntPtr password, UInt32 dwFlags, Boolean persistKeySe
t, SafeCertContextHandle& pCertCtx)
   at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromFile(String fileName, Object password, X509KeyStorageFlags keyS
torageFlags)
   at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(String fileName)
   at CORSIS.PortFusion.Security.Tool.view@167-1.Invoke(String file) in C:\CORSIS\Software\PortFusion\0.9.3\PortFusionSource\PortFusion\SecurityTool\P
rogram.fs:line 170

   >  Private Key Password
   =  ""
   =  PS C:\CORSIS\Software\PortFusion\0.9.3\PortFusionSource\PortFusion\SecurityTool\bin\Debug>

How can I tell the two instance of System.Security.Cryptography.CryptographicException apart?

Note: exception texts get localized so I cannot match on them as descriptions would be different on German, Chinese machines etc.

Cetin Sert
  • 4,497
  • 5
  • 38
  • 76
  • The `Message` would be different for the different types - you will have to have localized versions of the test though :( – Oded Jan 01 '12 at 18:45
  • 1
    You could also compare the `.StackTrace`s and check for e.g. _LoadCertFromFile – The Nail Jan 01 '12 at 18:53
  • See also http://msdn.microsoft.com/en-us/library/system.security.cryptography.cryptographicexception.aspx, maybe you can differentiate by error code or inner exception. – The Nail Jan 01 '12 at 18:56

3 Answers3

3

You are right to avoid using message strings programmatically to differentiate exceptions. Your best bets to differentiate exceptions that use the same type are the following:

  • The Data dictionary; this is not used all that often, but you can check if there are any data values that differ between the types.
  • The InnerException type (if there is one).

The HResult value is useful but is not accessible (it is a protected property -- you would need reflection to read it). It also tends to be the same value for the same exception type.

I should point out that more often than not, if two exceptions have the same runtime type, they are generally expected to be handled the same way. Evaluate if you really need to handle them differently.

bobbymcr
  • 23,769
  • 3
  • 56
  • 67
2

I didn't test it but Exception.HResult property looks promising: MSDN

Novakov
  • 3,055
  • 1
  • 16
  • 32
  • HResult is protected and these exceptions are thrown by system classes. I can do reflection to see if they differ in my case though. – Cetin Sert Jan 01 '12 at 18:58
  • 3
    See the answer for http://stackoverflow.com/questions/991537/how-do-i-determine-the-hresult-for-a-system-io-ioexception, it uses `Marshal.GetHRForException` – The Nail Jan 01 '12 at 19:02
  • 1
    Interesting approach with the `Marshal` class -- although this has side effects that might be undesirable (setting the COM `IErrorInfo` data for the calling thread). – bobbymcr Jan 01 '12 at 19:06
0

Their Message properties will be different.

dahlbyk
  • 75,175
  • 8
  • 100
  • 122