System.Security.Cryptography.CryptographicExceptio n

Discussion in 'ASP.NET / ASP.NET Core' started by sdstech, Jan 22, 2010.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. Hi, I have an issue of a somewhat urgent nature. Thanks in advance for any assistance offered.

    When I first switched over to discountasp.net (from another .NET hosting provider, at which I had no issues with the same code), I started receiving this exception:

    Code:
    System.Security.Cryptography.CryptographicException: The system cannot find the file specified.
       at System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer)
       at System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(RSAParameters parameters)
       at System.Security.Cryptography.RSA.FromXmlString(String xmlString)
    I found some advice that suggested this was a permissions-related issue having to do with the user key store, so I now set .UserMachineKeyStore to true. That made the problem go away... or so I thought.

    I still log this exception intermittently, and rather infrequently (4 times in the last 2 weeks, out of 100's to 1000's of calls), but it is indeed still happening. Here's all that I'm doing; the exception is thrown on the .FromXmlString() call:

    Code:
    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
    RSACryptoServiceProvider.UseMachineKeyStore = true; // important for discountasp.net
    rsa.FromXmlString(_privateKey);
    It's worth mentioning that since my move to discountasp.net I've also added a <machineKey> set in my web.config although I believe that was intended to solve a viewstate issue.

    Please help, I need this issue to disappear fast and can't reproduce it anywhere else.

    Thanks!
     
  2. Bruce

    Bruce DiscountASP.NET Staff

    Switching to MachinekeyStore is definitely right way to go.

    You mentioned you get some sporadic errors. Did you capture the error?
     
  3. Hey Bruce,

    Thanks for the reply, I posted the exception text above + code snippet for those sporadic errors.

    Thanks!
     
  4. Bruce

    Bruce DiscountASP.NET Staff

    So the error you are getting occasionally is

    "System.Security.Cryptography.CryptographicException: The system cannot find the file specified."

    Is there any way to replicate it?
     
  5. Hi bruce,

    Correct, that's the exception.

    And no, there's no way to replicate it reliably, it just shows up in my error logs whenever it wants to. Always the same stack trace, always the same code path, no variables or other application state that could make it programatically behave differently from one case to the next. It just doesn't work sometimes...

    EDIT: Let me rephrase that a bit, if I remove the .UseMachineKey = true then this problem can be reproduced with 100% reliability. Of course, this is in theory... since my production website is on your server, setting up this condition will cause me significant headaches and abandoned users. ;)
     
  6. Bruce

    Bruce DiscountASP.NET Staff

    that is so weird.. i have not heard of this problem after you set the useMachineKey setting to true.

    I'll do some research and post back if i find something..
     
  7. Hi Bruce,

    Any ideas?

    I'd hate to have to leave your service so soon, I just got here. But, all of my new users pass thru this code and it's enough for them never to come back if it errors out -- can't have that. :)
     
  8. Bruce

    Bruce DiscountASP.NET Staff

    sorry man.. i tried but i couldn't figure out anything.

    the thing is.. the error can't be replicated. it is almost impossible for us to track down what happened.
     
  9. If it helps...

    I've logged a few more of these errors since I started this thread, the most recent of which being @ 1/28/2010 10:19:48 PM (server time). The same user, 3 minutes later, successfully got thru this code with all the same variables in play.

    I'm going to list out the other timestamps in my log table here as maybe it will trigger a thought on your end. There does seem to be a grouping of times as many of these occur around the same time of day, which makes me wonder if you guys are running some sort of scheduled backup or other task that might cause intermittent behavior. Maybe you could take a few minutes and check out your server event log to see if there were any reboots or other obvious activities that occurred during these times?

    1/9/2010 11:47:23 PM
    1/10/2010 11:28:39 PM
    1/18/2010 7:40:13 PM
    1/19/2010 7:13:20 PM
    1/20/2010 11:58:15 PM
    1/22/2010 10:50:36 PM
    1/24/2010 10:47:55 AM
    1/24/2010 10:57:20 AM
    1/28/2010 10:19:48 PM

    Thanks!

    EDIT: another thought, would a move to another server make sense? I really would like to stick it out with you guys... I'm trying. :)
     
  10. Bruce, I think this is my fault. I was staring at this tonight for a while and realized that I was setting RSACryptoServiceProvider.UseMachineKeyStore after I constructed the RSACryptoServiceProvider. Since the failure occurs on the third line of the snippet above, this would mean one exception every time the web app restarts, which would explain the intermittence.

    Your thoughts?
     
  11. Bruce

    Bruce DiscountASP.NET Staff

    Try use user defined machineKey in your web.config. I suspect the key gets generated everytime the app restarts.
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page