简体   繁体   English

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

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

I am getting this error now whenever I try to build. 每当我尝试构建时,我都会收到此错误。 I just installed Visual Studio 2012 and .Net 4.5, but this project is still on 2010. 我刚刚安装了Visual Studio 2012和.Net 4.5,但这个项目仍然在2010年。

Here is the line of code I am having issues with: 以下是我遇到问题的代码行:

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

I receive an ArgumentException was unhandled by user code error saying, "Object of type 'System.Int32' cannot be converted to type 'System.Web.Security.Cryptography.Purpose'" Nothing has changed in my dev environment and my co-workers are not having the same problem, but they also do not have VS2012. 我收到一个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。

I found an article about Sitecore having this error, but this is the only place I have seen it pop up. 我发现一篇关于Sitecore 的文章有这个错误,但这是我见过的唯一一个弹出的地方。

There they say, "This is because in .NET 4.5 there are some new namespaces in System.Web " 他们在那里说,“这是因为在.NET 4.5中,System.Web中有一些新的命名空间”

Their solution is to: 他们的解决方案是:

  • Uninstall VS11 if you have it installed 如果安装了VS11,请将其卸载
  • Uninstall .NET 4.5 卸载.NET 4.5
  • Reinstall .NET 4 重新安装.NET 4

This seem like a ridiculous solution that 4.5 and 4 cant be on the same machine. 这似乎是一个荒谬的解决方案,4.5和4不能在同一台机器上。

Does anyone know what may be causing this and any better solutions before I try to un-install and re-install a bunch of stuff? 在我尝试卸载并重新安装一堆东西之前,是否有人知道可能导致此问题以及任何更好的解决方案?

A comment also says to try: </setting name="login.rememberlastloggedinusername" value="false" > but I don't want to do that either. 评论还说要尝试: </setting name="login.rememberlastloggedinusername" value="false" >但我也不想这样做。

As @hvd alluded to, this code is using reflection to call internal methods which Microsoft changed in .NET 4.5. 正如@hvd所提到的,这段代码使用反射来调用Microsoft在.NET 4.5中更改的内部方法。

Fortunately .NET 4.0 introduced the System.Web.Security.MachineKey class with public Encode() and Decode() methods which accomplish basically the same thing as the internal methods in the CookieProtectionHelper . 幸运的是,.NET 4.0引入了带有公共Encode()Decode()方法的System.Web.Security.MachineKey类,这些方法基本上与CookieProtectionHelper的内部方法CookieProtectionHelper Note that cookies that were encrypted with CookieProtectionHelper.Encode() will not be able to be decrypted with MachineKey.Decode() . 请注意,使用CookieProtectionHelper.Encode()加密的cookie将无法使用MachineKey.Decode()进行解密。

Also note that in .NET 4.5, these methods are deprecated in favor of Protect() and Unprotect() . 另请注意,在.NET 4.5中,不推荐使用这些方法,而使用Protect()Unprotect()

Change value to false in web.config: 在web.config中将值更改为false:

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

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

Did you get that from here ? 你从这里得到吗?

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

This relies on internal implementation details of the .NET Framework that MS never promised would remain unchanged. 这依赖于.NET框架的内部实现细节,MS从未承诺将保持不变。 So yes, an in-place upgrade of the .NET Framework could very well make such code stop working. 所以,是的,.NET Framework的就地升级很可能会使这些代码停止工作。 That's not a bug in .NET 4.5. 这不是.NET 4.5中的错误。 That's a bug in your -- that -- code for relying on things you cannot rely upon. 这是你的代码中的一个错误 - 依赖于你不能依赖的东西。

And to solve it, stop using that method. 要解决它,请停止使用该方法。 If there is a public API that does what you want, use that. 如果有一个公共API可以执行您想要的操作,请使用它。 If there isn't, implement it yourself. 如果没有,请自行实施。

If you see this error whilst using the CMS software Ektron , the following is in their 8.7 release notes - 如果您在使用CMS软件Ektron时看到此错误,则以下是其8.7发行说明 -

71233—If you installed an 8.6.1 site and enabled cookie encryption in web.config (), then installed Microsoft .NET Framework 4.5, you saw this error: 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

is fixed. 是固定的。

As mentioned in the other answers, one solution is to rollback to .Net framework 4.0. 正如其他答案中所提到的,一种解决方案是回滚到.Net framework 4.0。 The other answers in this particular case with Ektron are to disable cookie encryption, or upgrade to 8.7. Ektron在这种特殊情况下的其他答案是禁用cookie加密,或升级到8.7。

暂无
暂无

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

相关问题 类型&#39;System.Int16&#39;的对象不能转换为类型&#39;System.Nullable`1 [System.Int32] - Object of type 'System.Int16' cannot be converted to type 'System.Nullable`1[System.Int32] “System.Int32”类型的对象无法转换为“System.UInt32&”类型 - Object of type 'System.Int32' cannot be converted to type 'System.UInt32&' Expression.Convert:'System.Int64'类型的对象无法转换为'System.Int32'类型 - Expression.Convert: Object of type 'System.Int64' cannot be converted to type 'System.Int32' 无法将类型为“ System.Reflection.ParameterInfo”的对象转换为类型为“ System.Int32”的对象 - Object of type 'System.Reflection.ParameterInfo' cannot be converted to type 'System.Int32' 无法将类型为“ System.Double”的对象转换为类型为“ System.Int32”的对象 - Object of type 'System.Double' cannot be converted to type 'System.Int32' ASP.NET FormView:“类型&#39;System.Int32&#39;的对象不能转换为类型&#39;System.String” - ASP.NET FormView: “Object of type 'System.Int32' cannot be converted to type 'System.String” 类型'System.Int32'的表达式不能用于返回类型'System.Object' - Expression of type 'System.Int32' cannot be used for return type 'System.Object' Linq和Equality Operator:类型'System.Int32'的表达式不能用于'System.Object'类型的参数 - Linq and the Equality Operator: Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' 类型'System.Int32'的表达式不能用于方法'Boolean Equals(System.Object)'的'System.Object'类型的参数 - Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)' 类型&#39;System.Int32&#39;的表达式不能用于方法&#39;Boolean Equals(System.Object)&#39;的类型&#39;System.Object&#39;的参数 - Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM