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

StackOverflow https://stackoverflow.com/questions/11831655

Pergunta

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.

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.

I found an article about Sitecore having this error, but this is the only place I have seen it pop up.

There they say, "This is because in .NET 4.5 there are some new namespaces in System.Web "

Their solution is to:

  • Uninstall VS11 if you have it installed
  • Uninstall .NET 4.5
  • Reinstall .NET 4

This seem like a ridiculous solution that 4.5 and 4 cant be on the same machine.

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.

Foi útil?

Solução

As @hvd alluded to, this code is using reflection to call internal methods which Microsoft changed in .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. Note that cookies that were encrypted with CookieProtectionHelper.Encode() will not be able to be decrypted with MachineKey.Decode().

Also note that in .NET 4.5, these methods are deprecated in favor of Protect() and Unprotect().

Outras dicas

Change value to false in web.config:

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

(from: 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. So yes, an in-place upgrade of the .NET Framework could very well make such code stop working. That's not a bug in .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. 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-

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:

 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. The other answers in this particular case with Ektron are to disable cookie encryption, or upgrade to 8.7.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top