Azure / Azure/azure-functions-host

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

Open
#7,592 10 comments 2 reactions 0 assignees View on GitHub
Needs: Attention :wave:
Dominant language
C#
Stars
2k
Forks
482
Avg merge
2d 12h
Merged PRs (30d)
38

Description

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**
```xml



```

**Startup.cs**

```C#
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**
```C#
[FunctionName("Function1")]
public static async Task 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](https://i.ibb.co/M2k0zHL/1.png)

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](https://i.ibb.co/s5sGmyf/2.png)

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** trick in the .csproj and it simply generated another error.

Any help would be appreciated!

Contributor guide

Open the contributing guide

Research direction

Start with the package references in the project file and the Startup.cs and Function1.cs examples, then reproduce the failure at AuthenticateAsync(). Compare the Microsoft.IdentityModel.Tokens versions brought in by Microsoft.Identity.Web and PnP.Core. Done means the Azure Function builds and AuthenticateAsync() succeeds with the documented package versions.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, csharp
Domain
backend, cloud
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.