Search code examples
c#asp.net-identity

Issues with TokenClient in IdentityModel


I have a new MVC project using TokenClient from IdentityModel

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

I have the nuget package in for IdentityModel and everything compiles fine. However, at runtime I get the following error.

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

The .NET version of the MVC project is 4.6.1

What can be causing this issue? I have been searching google and cannot find anything that helps. It must be something simple that i am missing.

EDIT:

initializing it by declaring the parameters explicitly does not work either.

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

However initializing it with the one parameter works fine.

var tokenClient = new TokenClient(tokenUrl);

Solution

  • IdentityModel is a 3rd party library built by the creators of Identity Server. v3.10.1 definitely does have that method overload in it. I have recreated your error and the reason you are getting the error is because IdentityModel v3.10.1 is not compatible with .NET Framework 4.6.1. The creators changed the signature of that overload and made the HttpMessageHandler an optional parameter so your code will compile, but will throw this Method Not Found error at runtime. The IdentityModel project you are referencing has been archived by the guys at Identity Server so I would recommend migrating if you can.

    You have a couple of options as I see it:

    1) Migrate to .NET Core and leverage IdentityModel v2.

    2) Downgrade your project to .NET Framework 4.5.2 (the last compatible version for IdentityModel V1)

    3) Do not use this overload (as you've already found the single tokenUrl param works). I would stay away from this approach as you are likely to run into additional compatibility issues.

    Basically, if you don't want to migrate to .NET Core, keep this project on 4.5.2. If you can migrate, do that instead. Identity Server is moving toward .NET Core as a whole anyway and you will get more mileage by making that leap now.