繁体   English   中英

“System.Int32”类型的对象无法转换为“System.Web.Security.Cryptography.Purpose”类型

[英]Object of type 'System.Int32' cannot be converted to type 'System.Web.Security.Cryptography.Purpose'

每当我尝试构建时,我都会收到此错误。 我刚刚安装了Visual Studio 2012和.Net 4.5,但这个项目仍然在2010年。

以下是我遇到问题的代码行:

private static MethodInfo _encode;
public static string Encode(CookieProtection cookieProtection, byte[] buf, int count)
{
  return (string)_encode.Invoke(null, new object[] { cookieProtection, buf, count });
}

我收到一个ArgumentException was unhandled by user code "Object of type 'System.Int32' cannot be converted to type 'System.Web.Security.Cryptography.Purpose'" ArgumentException was unhandled by user code错误ArgumentException was unhandled by user code"Object of type 'System.Int32' cannot be converted to type 'System.Web.Security.Cryptography.Purpose'"我的开发环境和我的同事没有任何改变没有同样的问题,但他们也没有VS2012。

我发现一篇关于Sitecore 的文章有这个错误,但这是我见过的唯一一个弹出的地方。

他们在那里说,“这是因为在.NET 4.5中,System.Web中有一些新的命名空间”

他们的解决方案是:

  • 如果安装了VS11,请将其卸载
  • 卸载.NET 4.5
  • 重新安装.NET 4

这似乎是一个荒谬的解决方案,4.5和4不能在同一台机器上。

在我尝试卸载并重新安装一堆东西之前,是否有人知道可能导致此问题以及任何更好的解决方案?

评论还说要尝试: </setting name="login.rememberlastloggedinusername" value="false" >但我也不想这样做。

正如@hvd所提到的,这段代码使用反射来调用Microsoft在.NET 4.5中更改的内部方法。

幸运的是,.NET 4.0引入了带有公共Encode()Decode()方法的System.Web.Security.MachineKey类,这些方法基本上与CookieProtectionHelper的内部方法CookieProtectionHelper 请注意,使用CookieProtectionHelper.Encode()加密的cookie将无法使用MachineKey.Decode()进行解密。

另请注意,在.NET 4.5中,不推荐使用这些方法,而使用Protect()Unprotect()

在web.config中将值更改为false:

<setting name=”Login.RememberLastLoggedInUserName” value=”false” /> 

(来自: http//truncatedcodr.wordpress.com/2012/06/20/fix-sitecore-and-net-framework-4-5/

你从这里得到吗?

_encode = cookieProtectionHelper.GetMethod(
    "Encode", BindingFlags.NonPublic | BindingFlags.Static);

这依赖于.NET框架的内部实现细节,MS从未承诺将保持不变。 所以,是的,.NET Framework的就地升级很可能会使这些代码停止工作。 这不是.NET 4.5中的错误。 这是你的代码中的一个错误 - 依赖于你不能依赖的东西。

要解决它,请停止使用该方法。 如果有一个公共API可以执行您想要的操作,请使用它。 如果没有,请自行实施。

如果您在使用CMS软件Ektron时看到此错误,则以下是其8.7发行说明 -

71233-如果您在web.config()中安装了8.6.1站点并启用了cookie加密,然后安装了Microsoft .NET Framework 4.5,则会看到以下错误:

 Server Error in '/' Application. 
 Object of type 'System.Int32' cannot be converted to type System.Web.Security.Cryptography.Purpose'. This

是固定的。

正如其他答案中所提到的,一种解决方案是回滚到.Net framework 4.0。 Ektron在这种特殊情况下的其他答案是禁用cookie加密,或升级到8.7。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM