Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 320
JCisar Avatar asked Aug 06 '12 15:08

JCisar


2 Answers

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().

like image 141
John Rutherford Avatar answered Oct 08 '22 14:10

John Rutherford


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/)

like image 38
Jeremiah Flaga Avatar answered Oct 08 '22 13:10

Jeremiah Flaga