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"条目
Oza*_*RAM 18
如果您使用表单身份验证.您可以在捕获异常时注销,并允许您的用户登录并创建有效的cookie
catch (CryptographicException cex)
{
FormsAuthentication.SignOut();
}
Run Code Online (Sandbox Code Playgroud)
小智 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
在文档中,以下是该属性的允许值:
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 中捕获错误,只要您使用表单身份验证(登录名/密码)。为我工作。
归档时间: |
|
查看次数: |
54445 次 |
最近记录: |