Azure / Azure/azure-functions-core-tools

Host crashes due to dependency on System.IdentityModel.Tokens.Jwt 6.35.0

Open
#3,770 6 comments 0 reactions 1 assignee Assigned to @soninaren View on GitHub
needs-investigation potential-bug
Dominant language
C#
Stars
1.5k
Forks
498
Avg merge
5d 4h
Merged PRs (30d)
15

Description

### Version

PS C:\source\> func --version
4.0.5907
PS C:\source\> dotnet --info
.NET SDK:
Version: 8.0.303
Commit: 29ab8e3268
Workload version: 8.0.300-manifests.7ccdde63
MSBuild version: 17.10.4+10fbfbf2e

Runtime Environment:
OS Name: Windows
OS Version: 10.0.22631
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.303\

.NET workloads installed:
[aspire]
Installation Source: SDK 8.0.300
Manifest Version: 8.0.2/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.0.2\WorkloadManifest.json
Install Type: FileBased

Host:
Version: 8.0.7
Architecture: x64
Commit: 2aade6beb0

.NET SDKs installed:
8.0.303 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 8.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 8.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

### Description

My application worked fine about two weeks ago. I'm not sure if it was the most recent .Net SDK update or the last AFCT update, but now I have an application, which uses the Microsoft.GraphClient, that no longer runs locally despite no code updates due to the following error:

```
[2024-07-16T01:10:54.847Z] An unhandled host error has occurred.
[2024-07-16T01:10:54.848Z] System.Private.CoreLib: Exception has been thrown by the target of an invocation. Microsoft.AspNetCore.Authentication.JwtBearer: Could not load file or assembly 'System.IdentityModel.Tokens.Jwt, Version=6.35.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
```

My first thought was to update all of the libraries to the latest, which I have done, but the issue persists. I [read a SO answer](https://stackoverflow.com/a/77121911/461099) that mentioned there was a change in that library from v6 to v7 that deals with a migration off of Newtonsoft, which I am not using. I tried the suggestion of explicitly using 7.6.3 of the OpenId Connect library, but no luck. My csproj is as follows:

```xml


net8.0
v4
Exe
enable
enable














PreserveNewest


PreserveNewest
Never




```

My Program.cs looks like this:

```csharp
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;

var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices(services => {
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
services.AddLogging();
services.AddHttpClient();
})
.Build();

host.Run();
```

Lastly my function looks like this:

```csharp
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using My.Client.Models;
using System.Text.Json;
using Microsoft.Graph;
using Azure.Identity;
using Microsoft.Graph.Models;
using Microsoft.Graph.Users.Item.SendMail;

namespace My.Client;

public class AppointmentRequest(ILogger logger, IHttpClientFactory httpClientFactory)
{
private readonly ILogger _logger = logger;
private readonly HttpClient _client = httpClientFactory.CreateClient();

[Function("AppointmentRequest")]
public async Task Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync().ConfigureAwait(false);
Appointment data;

try {
data = JsonSerializer.Deserialize(requestBody) ?? new Appointment();
} catch (JsonException) {
return new BadRequestResult();
}

var userId = Environment.GetEnvironmentVariable("userId", EnvironmentVariableTarget.Process);
var email = Environment.GetEnvironmentVariable("email", EnvironmentVariableTarget.Process);

GraphServiceClient graphServiceClient = GetGraphServiceClient();

var easternTime = TimeZoneInfo.ConvertTimeFromUtc(DateTimeOffset.UtcNow.DateTime,
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));

await graphServiceClient.Users[userId]
.SendMail
.PostAsync(new SendMailPostRequestBody {
Message = new Message {
Subject = "Appointment Request",
Body = new ItemBody {
ContentType = BodyType.Html,
Content = $@""
},
ToRecipients = new List {
new Recipient {
EmailAddress = new EmailAddress {
Address = email
}
}
}
},
SaveToSentItems = false
}).ConfigureAwait(false);
_logger.LogInformation("mail appointment");

return new StatusCodeResult(StatusCodes.Status201Created);
}

private static GraphServiceClient GetGraphServiceClient() {
var clientId = Environment.GetEnvironmentVariable("clientId", EnvironmentVariableTarget.Process);
var clientSecret = Environment.GetEnvironmentVariable("clientSecret", EnvironmentVariableTarget.Process);
var tenantId = Environment.GetEnvironmentVariable("tenantId", EnvironmentVariableTarget.Process);

var scopes = new string[] { "https://graph.microsoft.com/.default" };

var options = new ClientSecretCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};

var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);

var graphServiceClient = new GraphServiceClient(clientSecretCredential, scopes);

return graphServiceClient;
}
}
```

### Steps to reproduce

Run `func start`
Invoke the endpoint

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.