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

Jer*_*man 37 .net c# asp.net cryptographicexception

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

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


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

码:

/// <summary>
    /// This method removes a cookie if the machine key is different than the one that saved the cookie;
    /// </summary>
    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();
        }
    }
Run Code Online (Sandbox Code Playgroud)


堆栈跟踪:

[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
Run Code Online (Sandbox Code Playgroud)

Gra*_*ace 26

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

  • 你能扩展这个答案吗?你使用了什么特定的配置?这个可以用iis配置吗? (3认同)

Baq*_*qvi 26

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


Oza*_*RAM 18

如果您使用表单身份验证.您可以在捕获异常时注销,并允许您的用户登录并创建有效的cookie

catch (CryptographicException cex)
{
    FormsAuthentication.SignOut();
}
Run Code Online (Sandbox Code Playgroud)

  • 您好,我有同样的问题,您能告诉我在哪里可以将此代码放入我的控制器中以捕获此错误吗? (3认同)

小智 7

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

在IIS中设置机器;

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


Man*_*til 7

另一种选择是从浏览器设置中清除 cookie,这允许存储新的 cookie。


and*_*ico 6

我在开发新解决方案并在本地主机上运行网站时也遇到过这种情况。设置 machinekey 没有任何区别,但只需删除 localhost 的所有 cookie 即可解决问题。


小智 6

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

<machineKey 
...
compatibilityMode="Framework20SP2"/>
Run Code Online (Sandbox Code Playgroud)

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

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

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


Jim*_*lff 6

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


bar*_*ost 5

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

在您的 Global.asax.cs 中,从在 Global.asax 中捕获错误,只要您使用表单身份验证(登录名/密码)。为我工作。