azure-functions-host icon indicating copy to clipboard operation
azure-functions-host copied to clipboard

Could not load file or assembly 'Microsoft.IdentityModel.Tokens, Version=6.12.0.0...

Open spplante opened this issue 4 years ago • 10 comments

Hi there,

Simply trying to use Microsoft.Identity.Web nuget with a brand new Azure Function v3 and I get dependencies issues at build/or runtime depending on the version of the nugets.

  • Create a brand new Azure Function v3
  • Add the latest Microsoft.Azure.Functions.Extensions
  • Add the latest Microsoft.Identity.Web nuget

Packages

    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
    <PackageReference Include="Microsoft.Identity.Web" Version="1.15.2" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.13" />

Startup.cs

public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            var config = ...
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddMicrosoftIdentityWebApi((JwtBearerOptions jwtBearerOptions) => { }, (MicrosoftIdentityOptions msiOptions) =>
            {
                msiOptions.Instance = "https://login.microsoftonline.com/";
                msiOptions.TenantId = "...";
                msiOptions.Domain = "...";
                msiOptions.ClientId = "...";
            });
        }
    }

Function1.cs

[FunctionName("Function1")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log)
{
	AuthenticateResult? result = await req.HttpContext.AuthenticateAsync("Bearer");

	if (!result.Succeeded)
		return new StatusCodeResult(StatusCodes.Status401Unauthorized);
	if (!result.Principal.IsInRole("CallAPI"))
		return new StatusCodeResult(StatusCodes.Status403Forbidden);

	return new OkObjectResult("hehe");
}

With this simple helloworld example the latest Microsoft.Identity.Web nuget is broken right out of the box. The AuthenticateAsync() method fails like so :

First error

When looking in the bin folder, the Microsoft.IdentityModel.Token dll is perfectly in place with the proper 6.12.0 version.

After messing with the packages, I managed to stop this random error by downgrading the Microsoft.Identity.Web nuget to 1.14.1 which made my code run 100% perfectly.

Until I added another package! After adding the PnP.Core v1.3.0 nuget, without any additional line of code, the AuthenticateAsync() now runs without throwing but its output now return the following error :

Second error

This time it's complaining about Microsoft.IdentityModel.Token that can't be loaded using version 6.11.1 and obviously this is the exact version sitting in the bin folder.

I tried downgrading both nugets, I tried explicitely installing Microsoft.IdentityModel.Token, I tried the <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput> trick in the .csproj and it simply generated another error.

Any help would be appreciated!

spplante avatar Aug 09 '21 12:08 spplante

Hi @spplante, Thank you for your feedback! We will check for the possibilities internally and update you with the findings.

v-bbalaiagar avatar Aug 10 '21 10:08 v-bbalaiagar

Thanks!

spplante avatar Aug 10 '21 12:08 spplante

@v-bbalaiagar any update on this? Thanks!

spplante avatar Aug 16 '21 13:08 spplante

Hi @spplante, Apologies for the delayed response, I checked on this issue internally. This is not a supported assembly in the current version of the host. This is flagged as a runtime restricted assembly. This is essentially combined with the framework assemblies, so it's interpreted as a framework assembly, you have to align with the framework version that is supported by the runtime.

v-bbalaiagar avatar Aug 16 '21 16:08 v-bbalaiagar

@v-bbalaiagar Not sure I understand, you are saying the Microsoft.Identity.Web assembly is not supported in the current version of the host because it is for some reason being interpreted as a framework assembly?

And what do you mean by I have to align with the framework version that is supported by the runtime? The runtime runs .NetCoreApp v3.1 and the Microsoft.Identity.Web supports .NetCoreApp v3.1, what am I missing here?

Thanks!

spplante avatar Aug 16 '21 16:08 spplante

@v-bbalaiagar sorry for bumping this but I can't go in prod without securing my Function App so it kinda is a show stopper for me here so I want to make sure I fully understand what you said 👍

spplante avatar Aug 18 '21 16:08 spplante

Hi @spplante, Apologies for the confusion. I mentioned above about Microsoft.Identity.Web assembly.

Tagging @balag0, for further insights.

v-bbalaiagar avatar Aug 24 '21 10:08 v-bbalaiagar

I've just encountered the same thing. I just redeployed my Functions with no changes, and then got the FileNotFoundException. I was running 1.14.0. Downgrading to 1.13.1 made it work again.

miklen avatar Sep 11 '21 10:09 miklen

Just tried this again with a new solution, this time using Microsoft.Identity.Web 1.20.0 and still the same issue.

Downgraded to 1.14.1 and it works since this time I don't need the PnP.Core v1.3.0 nuget, crossing fingers I won't get the issue after installing another nuget

spplante avatar Nov 19 '21 15:11 spplante

Running into this after adding redis package to my function, adding <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput> works, but I'm afraid of what side effects this might generate. Is there any update on this issue whether it's going to be solved ever since the last updates about it we're years ago?

raymondbrink avatar Jun 18 '25 08:06 raymondbrink