Authenticating Azure AD Function fails with "Could not load file or assembly 'System.IdentityModel.Tokens.Jwt, Version=6.29.0.0, The system cannot find the file specified."

Siegfried Heintze 1,906 Reputation points
2023-04-28T03:05:20.62+00:00

History:

I'm trying to write a compiled C# azure function that authenticates (and eventually authorizes) with Azure AD B2C This azure function will be protected by an Azure API Mgt. After a lot of fussing and cursing, I have a C# script azure function (as per this tutorial: https://learn.microsoft.com/en-us/azure/api-management/howto-protect-backend-frontend-azure-ad-b2c#build-the-function-api) that fetches the authentication header and decodes it to fetch the claims (to implement authorization) as discussed here: https://learn.microsoft.com/en-us/answers/questions/1240218/azure-ad-authenticated-azure-function-claims-to-be

I don't know why I cannot just fetch the claims from the http request: this works in MVC web apps but not in Azure (C# script) functions.

Since C# scripts die horribly when using the cosmos client (see https://learn.microsoft.com/en-us/answers/questions/1049462/azure-function-portal-c-script-cannot-fetch-nuget) I am creating a compiled C# azure function.

Current State of Affairs:

I need "System.IdentityModel.Tokens.Jwt" to decode the authentication header and this is working in the C# script. However, when I try to run the same code in a compiled C# azure function, I get this error:

WARNING: 2023-04-28T01:59:32.574 [Error] Executed 'Hello' (Failed, Id=ecb6cb4c-f99d-4532-9207-6e4e86e033d3, Duration=1ms) Could not load file or assembly 'System.IdentityModel.Tokens.Jwt, Version=6.29.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

I tried to uninstall this package and install it again using Visual Studio and deploy the compiled C# again but Visual Studio could not successfully deploy my C# code to the azure function any more (no error messages except a little warning icon in the portal that said "Azure Functions Runtime is unreachable").

So I deleted the azure function and deployed again with Visual Studio and was able to get the above error message from the logs again.

Please help me resolve this error so my azure function can authenticate and authorize.

Thanks

Siegfried

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,205 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
22,510 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. RevelinoB 3,505 Reputation points
    2023-04-28T04:23:28.9966667+00:00

    Hi Siegried, It looks like you are facing an issue with loading the System.IdentityModel.Tokens.Jwt assembly in your compiled C# Azure Function. This error occurs when the required assembly is not found or cannot be loaded by the function.

    To resolve this issue, please try to do the following steps if possible:

    Ensure that the System.IdentityModel.Tokens.Jwt assembly is referenced in your project. You can do this by right-clicking on your project in Visual Studio, selecting "Manage NuGet Packages," and searching for the System.IdentityModel.Tokens.Jwt package. If it is not listed, install it.

    Check the properties of the System.IdentityModel.Tokens.Jwt assembly reference in your project. Set the "Copy Local" property to true. This ensures that the assembly is copied to the output directory when building the project.

    If you have multiple projects in your solution, make sure that all the projects targeting the Azure Function have the System.IdentityModel.Tokens.Jwt assembly referenced and set to "Copy Local" as mentioned in step 2.

    Clean and rebuild your solution to ensure that the assembly is copied to the output directory.

    Ensure that you have the appropriate version of the System.IdentityModel.Tokens.Jwt assembly referenced in your project. Check the version specified in your error message (Version=6.29.0.0) and verify if you have that specific version installed. If not, try installing the required version or update the code to use a compatible version of the assembly.

    If the issue persists, try removing and re-adding the System.IdentityModel.Tokens.Jwt assembly reference. You can remove it from the project, clean the solution, and then add it again.

    Verify that the required assembly is deployed to the Azure Function App. You can check the "Dependencies" section in the Azure portal for your Function App and ensure that System.IdentityModel.Tokens.Jwt is listed as a dependency. If it is not listed, try redeploying your Azure Function from Visual Studio to ensure the correct deployment.

    After you followed these steps you should be able to resolve the assembly loading issue and successfully authenticate and authorize your Azure Function using Azure AD B2C.

    I hope this helps? Please let me know if you have any other queries?


  2. RevelinoB 3,505 Reputation points
    2023-05-01T04:19:19.6466667+00:00

    Hi Siegried, When I look at your code, the syntax you used in your .csproj file is incorrect for marking an assembly as local. To achieve this, you need to use the <PrivateAssets> attribute along with the CopyLocal value. To modify your .csproj file with the correct syntax:

    <Project>
      <!-- ... -->
      <ItemGroup>
        <!-- ... -->
        <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.29.0">
          <PrivateAssets>all</PrivateAssets>
          <CopyLocal>true</CopyLocal>
        </PackageReference>
      </ItemGroup>
      <!-- ... -->
    </Project>
    
    

    Try to update your .csproj file with these modifications and rebuild your Azure Function project.

    Regarding the deployment of Docker images to Azure Functions, it's important to note that using Docker images with Azure Functions is supported in the premium or dedicated plans. Unfortunately, the free and consumption plans have certain limitations, and they may not support all features, including custom Docker images.

    If you encounter difficulties with the "local copy" approach and the free plan, I recommend exploring alternative options. For example, you could consider using the "Copy Local Dependencies" feature in Visual Studio to handle the assembly copying automatically. Additionally, it might be worth exploring alternative methods for authentication and authorization in your Azure Function that don't rely on the System.IdentityModel.Tokens.Jwt package.

    Just bare in mind, and I don't know if you're open to it, but you could try the premium or dedicated plans, as they offer greater flexibility and feature support for deploying Docker images in Azure Functions.

    I hope this syntax will help you further?


  3. Siegfried Heintze 1,906 Reputation points
    2023-08-21T23:52:44.4566667+00:00

    Sorry for the delay. I was so happy to have resolved this that I forgot to post the solution.

    Answer:

    Since the ClaimsPrincipal argument does not work for C# script azure http trigger functions (I get claims, but not my claims) and it is necessary to insert the C# code to grab the authorization header and decode the JWT yourself, I assumed I would need to use the same approach for compiled C# azure functions.

    However, that does not work for compiled C# azure functions (see my above errors that I never was able to resolve).

    The claims principal argument does work for compiled C# azure http trigger functions, however. I get my claims.

    It took me a long time to figure this out!

    Thanks

    Siegfried


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.