【问题标题】:ASP.NET MVC tries to load older version of Owin assemblyASP.NET MVC 尝试加载旧版本的 Owin 程序集
【发布时间】:2014-07-27 03:41:05
【问题描述】:

作为上下文,我正在开发一个 ASP.NET MVC 5 应用程序,该应用程序通过 Microsoft 的 OWIN 实现使用基于 OAuth 的身份验证,目前仅适用于 Facebook 和 Google。目前(从 v3.0.0 开始,git-commit 4932c2f),FacebookAuthenticationOptionsGoogleOAuth2AuthenticationOptions 不提供任何属性来强制 Facebook 或 Google 在登录时分别重新验证用户(通过附加适当的查询字符串参数)。

最初,我着手重写以下类:

  • FacebookAuthenticationOptions
  • GoogleOAuth2AuthenticationOptions
  • FacebookAuthenticationHandler(特别是AuthenticateCoreAsync()
  • GoogleOAuth2AuthenticationHandler(特别是AuthenticateCoreAsync()

但发现~AuthenticationHandler 类被标记为internal

所以我提取了 Katana 项目 (http://katanaproject.codeplex.com/) 的源代码副本并相应地修改了源代码。

编译后,我发现有几个依赖项需要更新才能使用这些更新的程序集(Microsoft.Owin.Security.FacebookMicrosoft.Owin.Security.Google) 在 MVC 项目中:

  • Microsoft.Owin
  • Microsoft.Owin.Security
  • Microsoft.Owin.Security.Cookies
  • Microsoft.Owin.Security.OAuth
  • Microsoft.Owin.Host.SystemWeb

这是通过将现有项目引用替换为 3.0.0 版本并更新 web.config 中的引用来完成的。好消息:项目编译成功。

在调试中,我在启动时收到异常:

在 [MVC Web 程序集].dll 中发生“System.IO.FileLoadException”类型的异常,但未在用户代码中处理

其他信息:无法加载文件或程序集“Microsoft.Owin.Security, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。 (HRESULT 异常:0x80131040)

底层异常表明 Microsoft.AspNet.Identity.Owin 在从 @987654334 调用 app.UseExternalSignInCookie() 时试图加载 Microsoft.Owin.Security 的 v2.1.0 @ 在 Startup.Auth.cs 中。

不幸的是,该程序集(及其其他依赖项,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 包,这仍然是一个问题。

关于如何解决此问题的任何想法?

【问题讨论】:

  • 听起来您可能需要程序集绑定重定向。看看这个作为指南:rionscode.wordpress.com/tag/…
  • 谢谢,我读到它并意识到它所说的绑定重定向是我在 web.config 中所做的更改:<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 我已经检查并验证了引用的程序集确实是 3.0.0.0 ,但问题仍然存在。
  • @d_mcg - 您是否在绑定重定向中指定了程序集名称?对于要重定向到正确版本的每个包,您都需要绑定重定向。另外,您是否真的更新了项目中的引用或只是替换了 bin 文件夹中的 .dll?
  • 是的 - 有两个绑定重定向如下:<dependentAssembly> <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> 我还确保项目引用已相应更新。

标签: .net asp.net-mvc-5 assemblies asp.net-identity owin


【解决方案1】:

我更新到 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" />

【讨论】:

    【解决方案2】:

    我发现虽然FacebookAuthenticationHandler 被标记为internal,但它的抽象基类AuthenticationHandler&lt;TOptions&gt; 幸运的是public。最后,我从 Katana 项目源中获取了 FacebookAuthenticationHandler 的修改版本,将其重命名并包含在我自己的解决方案中,这样它仍然使用 2.1 库并且不会导致其他 NuGet 依赖项出现问题。

    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);
    
        ...
    }
    

    希望这可以帮助其他有类似问题的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-23
      • 2014-02-09
      • 2015-12-11
      • 2011-07-24
      相关资源
      最近更新 更多