ASP.NET MVC 嘗試載入舊版本的 Owin 程序集
作為上下文,我正在開發一個 ASP.NET MVC 5 應用程序,該應用程序通過 Microsoft 的 OWIN 實現使用基於 OAuth 的身份驗證,目前僅適用於 Facebook 和 Google。目前(從 v3.0.0 開始,git-commit 4932c2f),
FacebookAuthenticationOptions並且GoogleOAuth2AuthenticationOptions不提供任何屬性來強制 Facebook 或 Google 在登錄時分別重新驗證使用者身份(通過附加適當的查詢字元串參數)。最初,我著手重寫以下類:
FacebookAuthenticationOptionsGoogleOAuth2AuthenticationOptionsFacebookAuthenticationHandler(具體來說AuthenticateCoreAsync())GoogleOAuth2AuthenticationHandler(具體來說AuthenticateCoreAsync())但發現這些
~AuthenticationHandler類被標記為internal.因此,我提取了 Katana 項目 ( http://katanaproject.codeplex.com/ ) 的原始碼副本並相應地修改了原始碼。
編譯後,我發現有幾個依賴項需要更新才能在 MVC 項目中使用這些更新的程序集( Microsoft.Owin.Security.Facebook和Microsoft.Owin.Security.Google ):
- 微軟.Owin
- Microsoft.Owin.Security
- Microsoft.Owin.Security.Cookies
- Microsoft.Owin.Security.OAuth
- Microsoft.Owin.Host.SystemWeb
這是通過將現有項目引用替換為 3.0.0 版本並更新 web.config 中的引用來完成的。好消息:項目編譯成功。
在調試中,我在啟動時收到異常:
“System.IO.FileLoadException”類型的異常發生在
$$ MVC web assembly $$.dll 但未在使用者程式碼中處理 附加資訊:無法載入文件或程序集“Microsoft.Owin.Security,Version=3.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35”或其依賴項之一。找到的程序集的清單定義與程序集引用不匹配。(來自 HRESULT 的異常:0x80131040)
底層異常表明Microsoft.AspNet.Identity.Owin在從Startup.Auth.cs呼叫時試圖載入Microsoft.Owin.Security v2.1.0。
app.UseExternalSignInCookie()``Startup.ConfigureAuth(IAppBuilder app)不幸的是,該程序集(及其其他依賴項Microsoft.AspNet.Identity.Owin)不是 Project Katana 解決方案的一部分,我無法線上找到這些程序集的任何可訪問儲存庫。
Microsoft.AspNet.Identity程序集是否像 Katana 項目一樣開源?有沒有辦法欺騙這些程序集使用引用的 v3.0.0 程序集而不是 v2.1.0?該
/bin文件夾包含 3.0.0 版本的 Owin 程序集。我已經升級了Microsoft.AspNet.Identity.Owin的 NuGet 包,這仍然是一個問題。
關於如何解決這個問題的任何想法?
我發現雖然
FacebookAuthenticationHandler被標記為,但幸運的是internal它的抽象基類是。最後,我從 Katana 項目源中獲取了修改後的版本,將其重命名並包含在我自己的解決方案中,這樣它仍然使用 2.1 庫並且不會導致其他 NuGet 依賴項出現問題。AuthenticationHandler<TOptions>``public``FacebookAuthenticationHandler同樣對於
GoogleOAuth2AuthenticationHandler.因此,使用 Facebook 範例,我的 MVC 項目中有以下類定義:
// Overridden to add additional property 'ForceReauthentication' public class FacebookOAuth2ExtendedOptions : FacebookAuthenticationOptions ... // Reimplemented v2.1 source with modified method 'ApplyResponseChallengeAsync' public class FacebookOAuth2CustomHandler : AuthenticationHandler<FacebookOAuth2ExtendedOptions> ... // Reimplemented v2.1 source, modified to use 'FacebookOAuth2CustomHandler ' public class FacebookOAuth2CustomMiddleware : AuthenticationMiddleware<FacebookOAuth2ExtendedOptions> ...最後在
Startup類(App_Start/Startup.Auth.cs)中,我使用我的自定義中間件類註冊身份驗證:public void ConfigureAuth(IAppBuilder app) { ... var fbOptions = new FacebookOAuth2ExtendedOptions() { AppId = facebookAppKey, AppSecret = facebookAppSecret, ForceReauthentication = true }; app.Use(typeof(FacebookOAuth2CustomMiddleware), app, fbOptions); ... }希望這可以幫助其他有類似問題的人。
我更新到 OWin 3.0.1 並解決了問題:
工具 -> NuGet 包管理器 -> 管理解決方案的 NuGet 包
我搜尋了列表中的引用(在 NuGet.org 下)並安裝了 Microsoft.Owin 以及 Microsoft.Owin.Security、Microsoft.Owin.Security.Google、Microsoft.Owin.Security.Facebook 等的新引用。
然後,我檢查了 packages.config 和 Web.Config 文件中的版本號,發現它已正確更新:
Packages.config 範例:
<package id="Microsoft.Owin.Security.Google" version="3.0.1" TargetFramework="net45" />Web.Config 範例:
<assemblyIdentity name="Microsoft.Owin.Security" culture="neutral" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />