question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

System.IO.FileLoadException when running function ("Microsoft.IdentityModel.Tokens")

See original GitHub issue

Invocation ID: 4a8b83e2-2ad0-4f70-a13b-c41295cf6f17 Timestamp: 2018-02-21T16:25:54.224 Function App version (1.0 or 2.0-beta): 2.0-beta

Repro steps

Create a .net core project that contains the function to run.

Add package reference to project like below: <PackageReference Include="Microsoft.IdentityModel.Tokens" Version="5.2.1" />

Call a method that uses the above package and its dependancies.

Example method
public string CreateJWTToken(ClaimsIdentity identity)
{
    string _algorithm = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256";
    string _digest = "http://www.w3.org/2001/04/xmlenc#sha256";
    var symmetricKey = Encoding.UTF8.GetBytes("<somekey>");
    var securityKey = new SymmetricSecurityKey(symmetricKey);
    var credentials = new SigningCredentials(securityKey, _algorithm, _digest);

    var tokenDescriptor = new SecurityTokenDescriptor();
    tokenDescriptor.Subject = identity;
    tokenDescriptor.Issuer = "";
    tokenDescriptor.Audience = "";
    tokenDescriptor.SigningCredentials = credentials;

    var tokenHandler = new JwtSecurityTokenHandler();
    var token = tokenHandler.CreateToken(tokenDescriptor);
    return tokenHandler.WriteToken(token);
}

Expected behavior

Assembly resolved and jwt token created successfully

Actual behavior

Exception is thrown: System.IO.FileLoadException

Could not load file or assembly ‘Microsoft.IdentityModel.Tokens, Version=5.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’. Could not find or load a specific file. (Exception from HRESULT: 0x80131621)

Known workarounds

None

Related information

  1. This fails both on my local functions host and when running in Azure.
  2. I have inspected the bin folder in both cases and the assembly exists there right along side my application dll.
  3. I have tried to recreate in a .net core console app and all work well there.

Provide any related information

  • Programming language used C#
Binding
{
  "generatedBy": "Microsoft.NET.Sdk.Functions.Generator-1.0.8",
  "configurationSource": "attributes",
  "bindings": [
    {
      "type": "httpTrigger",
      "route": "Authenticate",
      "methods": [
        "post"
      ],
      "authLevel": "anonymous",
      "name": "request"
    }
  ],
  "disabled": false,
  "scriptFile": "../bin/MyProject.dll",
  "entryPoint": "MyProject.Functions.AuthenticateFunction.Run"
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
pizergcommented, May 11, 2018

I’m running into the same issue with Microsoft.IdentityModel.Tokens Version=“5.2.2”.

As a workaround I downgraded to version 5.1.4 which loads without problems

0reactions
fabiocavcommented, May 30, 2018

As mentioned. Work is in progress and fixes will be deployed with the next release. Leaving this open and moving to Sprint 25 for validation

Read more comments on GitHub >

github_iconTop Results From Across the Web

Azure function: Could not load file or assembly Microsoft. ...
An unhandled exception occurred while processing the request. FileLoadException: Could not load file or assembly 'System.IdentityModel.Tokens.
Read more >
Azure Function Could not load file or assembly ...
I implemented an application "Azure Functions" that makes use of "Microsoft.IdentityModel.Protocols.OpenIdConnect" to authenticate the session of a user, ...
Read more >
Resolving the "Could Not Load File or Assembly System. ...
A description on how I resolved the "Could not load file or assembly System.IdentityModel.Tokens.Jwt" in an Azure Functions application.
Read more >
Azure Functions How to fix Could not load file or assembly ...
Tokens. When this chunk of code runs I am getting the error "Could not load file or assembly 'Microsoft.IdentityModel.Tokens, Version=6.30.
Read more >
“Could not load file or assembly 'Microsoft.IdentityModel ...
“Could not load file or assembly 'Microsoft.IdentityModel.JsonWebTokens” when running PowerShell in Azure Functions.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found