“解密表单cookie时,”在加密操作期间发生错误“

我已经将我的网站上传到网站主办,这个错误出现了;
在加密操作期间发生错误 。’。

我做了一些研究,似乎将经过forms化的cookie绑定到MachineKey(使用webhost时会有所不同)。

我找到了一个方法来解决这个问题,但错误仍然存​​在。

码:

///  /// This method removes a cookie if the machine key is different than the one that saved the cookie; ///  protected void Application_Error(object sender, EventArgs e) { var error = Server.GetLastError(); var cryptoEx = error as CryptographicException; if (cryptoEx != null) { FederatedAuthentication.WSFederationAuthenticationModule.SignOut(); Global.Cookies.FormAuthenticated Cookie = new Global.Cookies.FormAuthenticated(); Cookie.Delete(); Server.ClearError(); } } 

堆栈跟踪:

 [CryptographicException: Error occurred during a cryptographic operation.] System.Web.Security.Cryptography.HomogenizingCryptoServiceWrapper.HomogenizeErrors(Func`2 func, Byte[] input) +115 System.Web.Security.Cryptography.HomogenizingCryptoServiceWrapper.Unprotect(Byte[] protectedData) +59 System.Web.Security.FormsAuthentication.Decrypt(String encryptedTicket) +9824926 Archive_Template.Main.resolveLoginUser(String sessionKey) in f:\Archive_Template\Archive_Template\Main.aspx.cs:481 Archive_Template.Main.OnPreInit(EventArgs e) in f:\Archive_Template\Archive_Template\Main.aspx.cs:52 System.Web.UI.Page.PerformPreInit() +31 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +335 

对于没有解决问题的人,我在web.config中缺少加密/解密的“machineKey”条目

我遇到了同样的问题。 我刚刚清除了所有浏览器的cookie缓存了数据,并且它已得到修复。我希望它也适合你。

如果您使用表单身份validation。 您可以在捕获exception时注销,并允许您的用户登录并创建有效的cookie

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

这是由于缺少机器密钥,它被用作对称密钥来进行加密和解密。

在IIS中设置机器;

转到您的应用程序 – >机器密钥 – >生成密钥

我刚刚有这个,我从数据库中删除了UserTokenCaches表条目。

在开发新解决方案并在localhost上运行网站时,我也遇到了这种情况。 设置machinekey没有区别,但只是删除localhost的所有cookie就解决了这个问题。

当我尝试使用ASP.NET 2.0应用程序创建的表单身份validationcookie并在.NET4.5 Web API项目中解密时,我遇到了这个问题。 解决方案是在web api的web.config文件中的“machineKey”节点中添加一个名为“compatibilityMode”的属性:

  

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

从文档中,以下是该属性的允许值:

  • Framework20SP1 。 此值指定ASP.NET使用早于2.0 SP2的ASP.NET版本中可用的加密方法。 如果任何服务器具有早于2.0 SP2的.NET Framework版本,请将此值用于Web场中的所有服务器。 除非应用程序Web.config文件将httpRuntime元素的targetFramework属性设置为“4.5”,否则这是默认值。
  • Framework20SP2 。 此值指定ASP.NET使用.NET Framework 2.0 SP2中引入的升级加密方法。 如果所有服务器都具有.NET Framework 2.0 SP2或更高版本但至少有一台服务器没有.NET Framework 4.5,请将此值用于Web场中的所有服务器。
  • Framework45 。 ASP.NET 4.5的加密增强function正在生效。 如果应用程序Web.config文件的httpRuntime元素的targetFramework属性设置为“4.5”,则这是默认值。

如果在实施单点登录时收到此错误(如此处所述http://www.alexboyang.com/2014/05/28/sso-for-asp-net-mvc4-and-mvc5-web-apps-shared-同域/ ),确保所有项目都有相同的目标框架。 我有一个项目使用.NET 4.0,另一个项目使用.NET 4.5.2。

将第一个更改为4.5.2为我解决了问题。

另一种选择是从浏览器设置中清除cookie,这样就可以存储新的cookie。

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

在您的Global.asax.cs中,来自Global.asax中的捕获错误 ,只要您使用Forms身份validation(登录/密码)。 为我工作。

validationAntiForgery令牌时,我收到了加密错误。

我相信这是因为我刚刚对我的服务器进行了一些安全控制配置更改,以便在虚拟内存限制达到1,000,000千字节时将应用程序回收配置为回收。

对于虚拟内存回收而言,这绝对是太少了。 专用内存使用量可以设置为1,000,000 KB,但虚拟内存应该有更多的空间。

我注意到我的应用程序经常回收。

我将虚拟内存限制增加到10,000,000 KB,这些错误消失了。 我相信应用程序池可能已经回收,因为我填写了表单。

我遇到了同样的问题:MVC 5 ASP.Net Web Application .net Framework 4.6.1

解:

  1. 转到App_Data文件夹(解决方案资源管理器)
  2. 双击NAME.mdf(此操作打开Server Explorer选项卡)
  3. 右键单击UserTokenCaches表并查看Show Table Data
  4. 删除该行
  5. 再次运行应用程序,一切都会好的