MSOnline Could not load type 'System.IdentityModel.Tokens.JwtSecurityToken'

%3CLINGO-SUB%20id%3D%22lingo-sub-356937%22%20slang%3D%22en-US%22%3EMSOnline%20Could%20not%20load%20type%20'System.IdentityModel.Tokens.JwtSecurityToken'%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-356937%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EI%20have%20an%20asp.net%20core%20web%20application%20that%20uses%20the%20%3CSTRONG%3EMSOnline%3C%2FSTRONG%3E%26nbsp%3BPowerShell%20module%20to%20interact%20with%20Office%20365.%20When%20the%20%3CSTRONG%3EConnect-MsolService%3C%2FSTRONG%3E%20cmdlet%20executes%20to%20authenticate%20with%20Office%20365%20I'm%20getting%20the%20following%20error.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CBLOCKQUOTE%3E%3CP%3ECould%20not%20load%20type%20'System.IdentityModel.Tokens.JwtSecurityToken'%20from%20assembly%20'System.IdentityModel.Tokens.Jwt%2C%20Version%3D5.1.5.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3D31bf3856ad364e35'.%3C%2FP%3E%3C%2FBLOCKQUOTE%3E%3CP%3E%3CSPAN%3EWhen%20the%20same%20logic%20executes%20in%20another%20project%20via%20a%20unit%20test%20I%20do%20not%20get%20an%20error%2C%20it%20works%20as%20expected.%20Both%20the%20test%20project%20and%20the%20web%20project%20are%20using%20the%20same%20version%20(5.1.5)%20of%20the%20%3CSTRONG%3ESystem.IdentityModel.Tokens.Jwt%3C%2FSTRONG%3E%20assembly%20so%20I%20don't%20understand%20why%20I'm%20getting%20this%20error%20when%20that%20cmdlet%20executes%20in%20the%20web%20app.%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3EDoes%20anyone%20know%20why%20this%20error%20would%20occur%20in%20an%20asp.net%20core%20web%20app%20but%20not%20a%20class%20library%3F%20I'm%20assuming%20one%20of%20the%20asp.net%20core%20nuget%20dependencies%20is%20causing%20this%20but%20I%20don't%20know%20why%20it%20would%3F%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EUpdate%3A%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EIt%20looks%20like%20my%20binding%20redirect%20is%20causing%20the%20problem.%20If%20I%20add%20the%20following%20to%20the%20app.config%20file%20in%20my%20test%20project%20it%20produces%20the%20error.%20This%20is%20very%20odd%20because%205.1.5%20is%20the%20version%20of%20%3CSTRONG%3ESystem.IdentityModel.Tokens.Jwt%3C%2FSTRONG%3E%20that's%20referenced%2C%20it's%20as%20if%20a%20different%20version%20is%20being%20used%20by%20default.%3C%2FP%3E%3CPRE%3E%3CRUNTIME%3E%3CBR%20%2F%3E%20%20%3CASSEMBLYBINDING%20xmlns%3D%22urn%3Aschemas-microsoft-com%3Aasm.v1%22%3E%3CBR%20%2F%3E%20%20%20%20%3CDEPENDENTASSEMBLY%3E%3CBR%20%2F%3E%20%20%20%20%20%20%3CASSEMBLYIDENTITY%20name%3D%22System.IdentityModel.Tokens.Jwt%22%20publickeytoken%3D%2231bf3856ad364e35%22%20culture%3D%22neutral%22%3E%3C%2FASSEMBLYIDENTITY%3E%3CBR%20%2F%3E%20%20%20%20%20%20%3CBINDINGREDIRECT%20oldversion%3D%220.0.0.0-5.4.0.0%22%20newversion%3D%225.1.5.0%22%3E%3C%2FBINDINGREDIRECT%3E%3CBR%20%2F%3E%20%20%20%20%3C%2FDEPENDENTASSEMBLY%3E%3CBR%20%2F%3E%20%20%3C%2FASSEMBLYBINDING%3E%3CBR%20%2F%3E%26lt%3B%2Fruntime%26gt%3B%3C%2FRUNTIME%3E%3C%2FPRE%3E%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-358427%22%20slang%3D%22en-US%22%3ERe%3A%20MSOnline%20Could%20not%20load%20type%20'System.IdentityModel.Tokens.JwtSecurityToken'%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-358427%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20case%20anyone%20is%20wondering%2C%20I%20found%20a%20workaround.%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fstackoverflow.com%2Fa%2F54894467%2F1121845%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fstackoverflow.com%2Fa%2F54894467%2F1121845%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

I have an asp.net core web application that uses the MSOnline PowerShell module to interact with Office 365. When the Connect-MsolService cmdlet executes to authenticate with Office 365 I'm getting the following error.

 

Could not load type 'System.IdentityModel.Tokens.JwtSecurityToken' from assembly 'System.IdentityModel.Tokens.Jwt, Version=5.1.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

When the same logic executes in another project via a unit test I do not get an error, it works as expected. Both the test project and the web project are using the same version (5.1.5) of the System.IdentityModel.Tokens.Jwt assembly so I don't understand why I'm getting this error when that cmdlet executes in the web app.

Does anyone know why this error would occur in an asp.net core web app but not a class library? I'm assuming one of the asp.net core nuget dependencies is causing this but I don't know why it would?

Update:

It looks like my binding redirect is causing the problem. If I add the following to the app.config file in my test project it produces the error. This is very odd because 5.1.5 is the version of System.IdentityModel.Tokens.Jwt that's referenced, it's as if a different version is being used by default.

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.IdentityModel.Tokens.Jwt" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.4.0.0" newVersion="5.1.5.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>

 

1 Reply