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.
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.MachineKeyclass with publicEncode()andDecode()methods which accomplish basically the same thing as the internal methods in theCookieProtectionHelper. Note that cookies that were encrypted withCookieProtectionHelper.Encode()will not be able to be decrypted withMachineKey.Decode().Also note that in .NET 4.5, these methods are deprecated in favor of
Protect()andUnprotect().