Azure function: Could not load file or assembly Microsoft.IdentityModel.Tokens, Version=5.2.1.0
Asked Answered
C

4

15

Im writing an azure function to generate a JWT token and return it to the client. The code is tested locally in a console app and all seems to work fine. This is the package reference included in the working console app, and in my functions app:

<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.2.1" />

When running the function host locally with func host start and executing the code it results in the error:

Could not load file or assembly 'Microsoft.IdentityModel.Tokens, Version=5.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'."

I don't understand why this is happening, the dll is laying in the output folder along with my application dll. The only other thing I can think of is that the function host has its own set of packages that it sources from and this one is not available yet, having been only released 12 days ago.

I'm not sure. Any help on why this is happening or how to get around it?

Details:
Azure Functions Core Tools (2.0.1-beta.22)
Function Runtime Version: 2.0.11415.0

Catchpole answered 20/2, 2018 at 16:51 Comment(3)
If there is demo code will be more helpful.Mahlstick
An unhandled exception occurred while processing the request. FileLoadException: Could not load file or assembly 'System.IdentityModel.Tokens.Jwt, Version=5.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.Sheff
I'm getting v.similar ... works locally, fails when pushed to azure ...Sheff
J
20

I got this issue and it seems to be related to some kind of bug in the Azure function SDK. the fix was to add:

<_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>

to your csproj file. As documented here

Jordanna answered 26/10, 2020 at 14:28 Comment(0)
C
7

I had installed this package Microsoft.AspNetCore.Authentication.JwtBearer

And for me, the issue was resolved.

You can uninstall System.IdentityModel.Tokens.Jwt

Because the Microsoft package depends on the system package, so it gets installed automatically.

Cog answered 27/9, 2021 at 13:31 Comment(1)
This worked for me as well. I had to take an earlier version since my app is running in .NET 6, but it got things moving.Refresher
S
1

I was able to solve this exact issue by using an older version of the nuget package. My starting point was that I had copied a class file from an old project to a new one. The class file referenced JwtSecurityToken. This did not compile in the new project, so I added Security.IdentityModel.Tokens.Jwt from nuget package manager. I just added latest version. This worked fine locally, but just like you, it failed when published to azure. I then looked at the old project and noticed that it was using 5.1.4 of that Security.IdentityModel.Tokens.Jwt. So, I downgraded to that version and it now works when published.

fwiw: this is the v2 preview runtime version at the time I did this.

https://<mysite>.azurewebsites.net/admin/host/status?code=<myadminkey>
{
"id": "<mysite>",
"state": "Running",
"version": "2.0.11587.0",
"versionDetails": "2.0.11587.0-beta1 Commit hash: 1e9e7a8dc8a68a3eff63ee8604926a8d3d1902d6"
}
Sheff answered 17/3, 2018 at 16:45 Comment(0)
H
0

tl;dr

None of the above worked for me and this would randomly happen from time to time until today it happened all the time. The only reason I could see was that Microsoft.IdentityModel.Tokens was not directly referenced in the executing project, but was on a referenced project. The library was sitting in the bin folder, it just wouldn't load.

Reference

Taking a clue from this solution to another problem I was able to resolve it like so:

Solution

Create a static constructor in the app's entry point class

static MainClass()
{
    AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}

Add the handler

private static System.Reflection.Assembly? CurrentDomain_AssemblyResolve(object? sender, ResolveEventArgs args)
{
    var domain = sender as AppDomain;
    var assemblies = domain.GetAssemblies();

    foreach(var assembly in assemblies)
    {
        if (assembly.FullName.IsEqualTo(args.Name))
        {
            return assembly;
        }
    }

    var folder = AppDomain.CurrentDomain.BaseDirectory;
    var name = args.GetLibraryName().Name.Split(Symbols.Comma).FirstOrDefault();
    var library = $"{name}.dll";
    var file = Path.Combine(folder, library);

    if (File.Exists(file))
    {
        return Assembly.LoadFrom(file);
    }

    return null;
}
Holt answered 21/2, 2023 at 22:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.