IdentityModel 中 TokenClient 的问题

Issues with TokenClient in IdentityModel

我有一个新的 MVC 项目,使用来自 IdentityModel

TokenClient
var tokenClient = new TokenClient(tokenUrl, clientId, CLIENT_SECRET, null, AuthenticationStyle.BasicAuthentication);

我有 IdentityModel 的 nuget 包,一切都可以正常编译。但是,在运行时出现以下错误。

Method not found: 'Void IdentityModel.Client.TokenClient..ctor(System.String, System.String, System.String, System.Net.Http.HttpMessageHandler, IdentityModel.Client.AuthenticationStyle)'.

MVC项目的.NET版本为4.6.1

是什么导致了这个问题?我一直在搜索 google,但找不到任何有用的信息。我想念的一定是简单的东西。

编辑:

通过显式声明参数来初始化它也不起作用。

var tokenClient = new TokenClient(tokenUrl, clientId: clientId, clientSecret: CLIENT_SECRET);// CLIENT_SECRET, null, AuthenticationStyle.BasicAuthentication);

然而用一个参数初始化它工作正常。

var tokenClient = new TokenClient(tokenUrl);

IdentityModel 是由 Identity Server 的创建者构建的第 3 方库。 v3.10.1 中确实有该方法重载。我已经重新创建了您的错误,您收到错误的原因是 IdentityModel v3.10.1 与 .NET Framework 4.6.1 不兼容。创建者更改了该重载的签名,并将 HttpMessageHandler 设为可选参数,以便您的代码可以编译,但会在 运行 时抛出此方法未找到错误。您引用的 IdentityModel 项目已被 Identity Server 的人员存档,因此我建议您尽可能进行迁移。

我认为你有几个选择:

1) 迁移到 .NET Core 并利用 IdentityModel v2。

2) 将您的项目降级到 .NET Framework 4.5.2(IdentityModel V1 的最后一个兼容版本)

3) 不要使用此重载(因为您已经发现单个 tokenUrl 参数有效)。我会远离这种方法,因为您可能 运行 会遇到其他兼容性问题。

基本上,如果您不想迁移到 .NET Core,请将此项目保留在 4.5.2 上。如果可以迁移,请改为迁移。无论如何,Identity Server 正在整体上转向 .NET Core,现在实现这一飞跃您将获得更多里程。

我在处理 IdentityServer3 的 MVC 入门示例时遇到了同样的问题。如果您检查 IdentityModel v3.10.1 的依赖项,您会注意到它依赖于 System.Net.Http (>= 4.3.3)。我的项目有 v 4.2,更新到当前版本解决了问题。

如果您正在使用 ASP.Net MVC 应用程序检查 System.Net.Http 中 web.config

的绑定重定向

应该是

 <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.0.0.0" />
 </dependentAssembly>