ASP.NET MVC 尝试加载旧版本的 Owin 程序集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24072168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:45:25  来源:igfitidea点击:

ASP.NET MVC tries to load older version of Owin assembly

.netasp.net-mvc-5assembliesasp.net-identityowin

提问by d_mcg

As a bit of context, I'm developing an ASP.NET MVC 5 application that uses OAuth-based authentication via Microsoft's OWIN implementation, for Facebook and Google only at this stage. Currently (as of v3.0.0, git-commit 4932c2f), the FacebookAuthenticationOptionsand GoogleOAuth2AuthenticationOptionsdon't provide any property to force Facebook nor Google respectively to reauthenticate users (via appending the appropriate query string parameters) when signing in.

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

Initially, I set out to override the following classes:

最初,我开始覆盖以下类:

  • FacebookAuthenticationOptions
  • GoogleOAuth2AuthenticationOptions
  • FacebookAuthenticationHandler(specifically AuthenticateCoreAsync())
  • GoogleOAuth2AuthenticationHandler(specifically AuthenticateCoreAsync())
  • FacebookAuthenticationOptions
  • GoogleOAuth2AuthenticationOptions
  • FacebookAuthenticationHandler(具体AuthenticateCoreAsync()
  • GoogleOAuth2AuthenticationHandler(具体AuthenticateCoreAsync()

yet discovered that the ~AuthenticationHandlerclasses are marked as internal.

却发现这些~AuthenticationHandler类被标记为internal.

So I pulled a copy of the source for the Katana project (http://katanaproject.codeplex.com/) and modified the source accordingly.

因此,我为 Katana 项目 ( http://katanaproject.codeplex.com/)提取了一份源代码副本,并相应地修改了源代码。

After compiling, I found that there are several dependencies that needed updating in order to use these updated assemblies (Microsoft.Owin.Security.Facebookand Microsoft.Owin.Security.Google) in the MVC project:

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

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

This was done by replacing the existing project references to the 3.0.0 versions and updating those in web.config. Good news: the project compiles successfully.

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

In debugging, I received an exception on startup:

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

An exception of type 'System.IO.FileLoadException' occurred in [MVC web assembly].dll but was not handled in user code

Additional information: Could not load file or assembly 'Microsoft.Owin.Security, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

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

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

The underlying exception indicated that Microsoft.AspNet.Identity.Owinwas trying to load v2.1.0 of Microsoft.Owin.Securitywhen calling app.UseExternalSignInCookie()from Startup.ConfigureAuth(IAppBuilder app)in Startup.Auth.cs.

基本的异常表明Microsoft.AspNet.Identity.Owin试图负载V2.1.0 Microsoft.Owin.Security打电话时app.UseExternalSignInCookie()Startup.ConfigureAuth(IAppBuilder app)在Startup.Auth.cs。

Unfortunately that assembly (and its other dependency, Microsoft.AspNet.Identity.Owin) aren't part of the Project Katana solution, and I can't find any accessible repository for these assemblies online.

不幸的是,该程序集(及其其他依赖项Microsoft.AspNet.Identity.Owin)不是 Project Katana 解决方案的一部分,我无法在线找到这些程序集的任何可访问存储库。

Are the Microsoft.AspNet.Identityassemblies open source, like the Katana project? Is there a way to fool those assemblies to use the referenced v3.0.0 assemblies instead of v2.1.0? The /binfolder contains the 3.0.0 versions of the Owin assemblies.

Microsoft.AspNet.Identity程序集是否像 Katana 项目一样是开源的?有没有办法欺骗这些程序集使用引用的 v3.0.0 程序集而不是 v2.1.0?该/bin文件夹包含 Owin 程序集的 3.0.0 版本。

I've upgraded the NuGet packages for Microsoft.AspNet.Identity.Owin, and this is still an issue.

我已经升级了Microsoft.AspNet.Identity.Owin的 NuGet 包,这仍然是一个问题。

Any ideas on how to resolve this issue?

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

采纳答案by d_mcg

I've found that while FacebookAuthenticationHandleris marked as internal, its abstract base class AuthenticationHandler<TOptions>is fortunately public. In the end, I took the modified version of FacebookAuthenticationHandlerfrom the Katana project source, renamed it and included it in my own solution, so that it still uses the 2.1 libraries and doesn't cause problems with the other NuGet dependencies.

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

Likewise for GoogleOAuth2AuthenticationHandler.

同样对于GoogleOAuth2AuthenticationHandler.

So, using the Facebook example, I have the following class definitions in my MVC project:

因此,使用 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>
...

Finally in the Startupclass (App_Start/Startup.Auth.cs), I register the authentication using my custom middleware classes:

最后在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);

    ...
}

Hope this helps others with similar issues.

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

回答by Dave Hatt

I updated to OWin 3.0.1 and that fixed the problem:

我更新到 OWin 3.0.1 并解决了这个问题:

Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution

工具 -> NuGet 包管理器 -> 管理解决方案的 NuGet 包

I searched for the references in the list (under NuGet.org) and installed new references for Microsoft.Owin as well as Microsoft.Owin.Security, Microsoft.Owin.Security.Google, Microsoft.Owin.Security.Facebook etc.

我搜索了列表中的引用(在 NuGet.org 下)并为 Microsoft.Owin 以及 Microsoft.Owin.Security、Microsoft.Owin.Security.Google、Microsoft.Owin.Security.Facebook 等安装了新的引用。

I then checked the version number in my packages.config and Web.Config file and found that this had updated correctly:

然后我检查了我的 packages.config 和 Web.Config 文件中的版本号,发现它已正确更新:

Packages.config example:

Packages.config 示例:

<package id="Microsoft.Owin.Security.Google" version="3.0.1" TargetFramework="net45" />

Web.Config example:

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