“Error occurred during a cryptographic operation” when decrypting Forms cookie

我只是一个虾纸丫 提交于 2019-11-28 21:05:25

For anyone who hasn't solved their problem, I was missing the "machineKey" entry for encrypt/decrypt in my web.config

I faced the same problem. I just cleared all of browser's cookies and cache data and it got fixed.I hope it will work for you too.

If you are using forms auth. you can signout when you catch the exception and allow your users to login and create a valid cookie

catch (CryptographicException cex)
{
    FormsAuthentication.SignOut();
}
Ghaleb Badran

This is due to the machine key is missing, which is used as a symmetric key to do the encryption and decryption.

To set the machine in the IIS;

Go to your application -> Machine Keys -> Generate Keys

I ran into this problem when I tried to take a forms authentication cookie created by an ASP.NET 2.0 app and decrypt it inside an .NET4.5 Web API project. The solution was to add an attribute called "compatibilityMode" to the "machineKey" node inside my web api's web.config file:

<machineKey 
...
compatibilityMode="Framework20SP2"/>

Documentation: https://msdn.microsoft.com/en-us/library/system.web.configuration.machinekeysection.compatibilitymode.aspx

And from the doc, here are the allowed values for that attribute:

  • Framework20SP1. This value specifies that ASP.NET uses encryption methods that were available in versions of ASP.NET earlier than 2.0 SP2. Use this value for all servers in a web farm if any server has a version of the .NET Framework earlier than 2.0 SP2. This is the default value unless the application Web.config file has the targetFramework attribute of the httpRuntime element set to "4.5".
  • Framework20SP2. This value specifies that ASP.NET uses upgraded encryption methods that were introduced in the .NET Framework 2.0 SP2. Use this value for all servers in a web farm if all servers have the .NET Framework 2.0 SP2 or later but at least one does not have the .NET Framework 4.5.
  • Framework45. Cryptographic enhancements for ASP.NET 4.5 are in effect. This is the default value if the application Web.config file has the targetFramework attribute of the httpRuntime element set to "4.5".

I just had this aswell, i deleted the UserTokenCaches table entries from the database.

I have also experienced this when developing a new solution and running the website on localhost. Setting the machinekey made no difference, but simply deleting all the cookies for localhost solved the problem.

       protected void Application_Error(object sender_, CommandEventArgs e_)
    {
        Exception exception = Server.GetLastError();
        if(exception is CryptographicException)
        {
            FormsAuthentication.SignOut();
        }
    }

in your Global.asax.cs, from Catching errors in Global.asax, as long as you use Forms authentication (login/password). Worked for me.

Another option is to clear the cookies from browser setting and this allows new cookies to get stored.

If you receive this error when implementing single sign on (as described here http://www.alexboyang.com/2014/05/28/sso-for-asp-net-mvc4-and-mvc5-web-apps-shared-the-same-domain/), make sure to have the same target framework across all projects. I had one project with .NET 4.0 and the other on .NET 4.5.2.

Changing the first one to 4.5.2 fixed the issue for me.

I was getting crypto errors when validating the AntiForgery token.

I believe it was because I had just made some security control configuration changes to my server to configure application recycling to recycle when Virtual Memory limits hit 1,000,000 Kilobytes.

This was definitely way too little for virtual memory recycling. Private memory usage can be set to 1,000,000 KB, but virtual memory should be given a lot more space.

I noticed my application was recycling much too often.

I increased the Virtual Memory limit to 10,000,000 KB and those errors went away. I believe the application pool may have been recycling as I was filling out the form.

I had the same issue: MVC 5 ASP.Net Web Application .net Framework 4.6.1

Solution:

  1. Go to App_Data folder (Solution explorer)
  2. Double click in your NAME.mdf (this action open Server Explorer Tab)
  3. Right click on UserTokenCaches table and view Show Table Data
  4. Delete the row
  5. Run app again and everything will be ok
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!