Azure Functions How to fix Could not load file or assembly 'Microsoft.IdentityModel.Tokens, Version=6.30.0.0...?

Jordi Dalmau 20 Reputation points
2023-05-03T21:45:01.4133333+00:00

I am making an Azure Durable Functions application that uses Microsoft.IdentityModel.Tokens.

When this chunk of code runs I am getting the error "Could not load file or assembly 'Microsoft.IdentityModel.Tokens, Version=6.30.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified."

private static string GenerateRandomString(int length)

{
    var bytes = RandomNumberGenerator.GetBytes(length);
    return Base64UrlEncoder.Encode(bytes);
}

I have seen another people having the same issue talking about an automatic system cleaning the package references when publishing. There are multiple suggestions about using the following lines in the .csproj.

<ItemGroup>
  <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
  <FunctionsPreservedDependencies Include="Microsoft.AspNetCore.Authentication.JwtBearer"/>
</ItemGroup>

But even after doing it and republishing the issue persists.

At this point I am stuck and cant put my application to work. I dont know what to do, the error seems unfixable and there is no proper documentation regarding this matter.

Here is my complete .csproj just in case anybody needs some extra context.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <RootNamespace>My_Application</RootNamespace>
    <DockerFastModeProjectMountDirectory>/home/site/wwwroot</DockerFastModeProjectMountDirectory>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
    <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.16" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="2.9.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="1.0.0" />
    <PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.30.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.1" />
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.2" />
  </ItemGroup>
  <ItemGroup>
    <FunctionsPreservedDependencies Include="Microsoft.AspNetCore.Authentication.JwtBearer"/>
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,929 questions
{count} votes

Accepted answer
  1. Ryan Hill 30,281 Reputation points Microsoft Employee Moderator
    2023-05-05T23:40:08.2733333+00:00

    I ran into same issue @Jordi Dalmau locally. The System.Security.Cryptography being referenced in Microsoft.IdentityModel.Tokens was 4.3.0 in my case which seemed quite "old". Running .NET 6, my assumption is binding failure. I removed that nuget package since System.Security.Cryptography is part of the runtime.

    After removing that reference, the function app ran locally and on Azure successfully.

    enter image description here

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.