Auth0 LoginAsync complains about newtonsoft.json dll version 10.0.0.0

I am facing a weird kind of situation while trying acheiving LoginAsync using Auth0.OidcclientWpf SDK. I am using below code:

    private async void PerformAuthenticationAsync()
    {
        Auth0Client auth0Client = new Auth0Client(new Auth0ClientOptions()
        {
             Domain = "mydomain",
             ClientId = "clientid"
        });

         var extraParameters = new Dictionary<string, string>();
         extraParameters.Add("connection", "connectionParam");
         var result = await auth0Client.LoginAsync(extraParameters: extraParameters);
    }

After performing authentication process. I am getting error:

Not able to load NewtonSoftJson.dll version - 10.0.0.0
and the source was Identitymodel.oidclient . Here after setting IdentityModelEventSource.ShowPII to True I can see in the trace that the token for which I am looking for is recieved but LoginResul t is failed to decode the header.

I tried refering NewtonSoft.Json dll version 10.0.2 in my project. Now I am getting exception like:

Not able to load NewtonSoftJson.dll version 11.0.0.0
and this time Browser was not even launched to enter credentials.

I am confused which version of NewtonSoft.Json dll I should refer? I updated my IdentityModel.oidcclient to the latest version. Even after that IdentityModel.Oidcclient.LoginResult is complaining for NewtonSoft.Json version 10.0.0.0 unavailability.

I am not sure what is the real issue. Whether I have to refer both the versions(10 & 11) and how to do that? I would appreciate if someone can help.

below is the trace I am getting -

System.TypeInitializationException: The type initializer for ‘System.IdentityModel.Tokens.Jwt.JsonExtensions’ threw an exception. —> System.IO.FileLoadException: Could not load file or assembly ‘Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) at System.IdentityModel.Tokens.Jwt.JsonExtensions…cctor() — End of inner exception stack trace — at System.IdentityModel.Tokens.Jwt.JsonExtensions.DeserializeJwtHeader(String jsonString) at System.IdentityModel.Tokens.Jwt.JwtSecurityToken.Decode(String[] tokenParts, String rawData) — End of inner exception stack trace — at System.IdentityModel.Tokens.Jwt.JwtSecurityToken.Decode(String[] tokenParts, String rawData) at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ReadJwtToken(String token) at IdentityModel.OidcClient.IdentityTokenValidator.d__4.MoveNext() >

I do have the same issue. Is there a solution for this.
The Assembly wants Newtonsoft.JSon 11 installed, and the Jwt wants 10 and I cannot have both. What is the solution here? I am using a WPF integration

        Auth0ClientOptions clientOptions = new Auth0ClientOptions
        {
            Domain = "***",
            ClientId = "****",
            RedirectUri = @"****",

            
        };
        Client = new Auth0Client(clientOptions);
        Client.LoginAsync()

I am also using a bindingredirect to 11 or 10, but nothing works

I solved it by forcing the library to load all instances of JSon with the JSon that is already loaded.
Then the problem changed to another library

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace NS {
    public class AssemblyLoadErrorResolver
    {
        #region Singleton
        /// <summary>
        /// Singleton implementation 
        /// </summary>
        public AssemblyLoadErrorResolver()
        {
            AppDomain currentDomain = AppDomain.CurrentDomain;

            currentDomain.AssemblyResolve += new ResolveEventHandler(ResolveAssemblyEventHandler);
        }

        public static string AssemblyDirectory
        {
            get
            {
                string codeBase = Assembly.GetExecutingAssembly().CodeBase;
                UriBuilder uri = new UriBuilder(codeBase);
                string path = Uri.UnescapeDataString(uri.Path);
                return Path.GetDirectoryName(path);
            }
        }

        private static Assembly ResolveAssemblyEventHandler(object sender, ResolveEventArgs args)
        {
            if (args.Name.StartsWith("System.Text.Encodings.Web"))
            {
                Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
                Assembly ns = assemblies.SingleOrDefault(a => a.FullName.Contains("System.Text.Encodings.Web"));
                // Assembly might not have been loaded yet.
                if (ns == null)
                {
                    ns = Assembly.LoadFile(Path.Combine(AssemblyDirectory, "System.Text.Encodings.Web.dll"));
                }
                return ns;
            }
            else if (args.Name.StartsWith("Newtonsoft.Json"))
            {
                Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
                Assembly ns = assemblies.SingleOrDefault(a => a.FullName.Contains("Newtonsoft.Json"));
                // Assembly might not have been loaded yet.
                if (ns == null)
                {
                    ns = Assembly.LoadFile(Path.Combine(AssemblyDirectory, "Newtonsoft.Json.dll"));
                }
                return ns;
            }
            else
            {
                return null;
            }
        }
    }
}
1 Like

Perfect! Thanks for sharing that with the rest of community!