簡體   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