IdentityModel中的TokenClient问题

时间:2018-11-20 15:28:00

标签: c# asp.net-identity

我有一个来自 IdentityModel

TokenClient 的新MVC项目
var tokenClient = new TokenClient(tokenUrl, clientId, CLIENT_SECRET, null, AuthenticationStyle.BasicAuthentication);

我在 IdentityModel 中安装了nuget包,并且一切正常。但是,在运行时,出现以下错误。

  

未找到方法:'无效   IdentityModel.Client.TokenClient..ctor(System.String,System.String,   System.String,System.Net.Http.HttpMessageHandler,   IdentityModel.Client.AuthenticationStyle)”。

MVC项目的.NET版本为4.6.1

什么可能导致此问题?我一直在搜索谷歌,找不到任何有帮助的东西。我想必一定是简单的事情。

编辑:

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

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

但是使用一个参数初始化它可以正常工作。

var tokenClient = new TokenClient(tokenUrl);

3 个答案:

答案 0 :(得分:2)

IdentityModel是由Identity Server的创建者构建的第三方库。 v3.10.1肯定确实有该方法的重载。我已经重新创建了您的错误,并且得到此错误的原因是因为IdentityModel v3.10.1与.NET Framework 4.6.1不兼容。创建者更改了该重载的签名,并将HttpMessageHandler设置为可选参数,以便您的代码可以编译,但会在运行时引发此Method Not Found错误。您正在引用的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,通过立即实现这一点,您将获得更多收益。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

如果您使用的是ASP.Net MVC应用程序,请在web.config中检查“ System.Net.Http ”的绑定重定向

应该像

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