简体   繁体   中英

Reference to a class library (.Net framework) method from .Net core API is throwing an error

I have a Web API project whose target framework is .Net Core 2.1 . The project has reference to a class library built on .Net Framework 4.5.2 . While calling the method on the class library which basically encodes / decodes a given value is throwing the following run time error.

Could not load type 'System.Security.Cryptography.MACTripleDES' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.":"System.Security.Cryptography.MACTripleDES"

Method defined on class Library:

    public string Encryptor(string ID)
    {
        string EncrytionKey = "sampleKey";
        MACTripleDES mac3des = new MACTripleDES();
        MD5CryptoServiceProvider md5 = MD5CryptoServiceProvider();
        mac3des.Key = md5.ComputeHash(Encoding.UTF8.GetBytes(EncrytionKey));

        return Convert.ToBase64String(Encoding.UTF8.GetBytes(ID)) + '-' + Convert.ToBase64String(mac3des.ComputeHash(Encoding.UTF8.GetBytes(ID)));

    }        

I could see similar questions on SO, suggesting to add project.json file to the project but I am not clear. Neither my Web API nor class library has a project.json file in it.

Please provide any insights on it.

What worked for me seems to be:

  1. Upgrading the MACTripleDES version: NuGet package.config file:
  2. Provide assembly binding redirects: <dependentAssembly> <assemblyIdentity name="System.Security.Cryptography.MACTripleDES" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.0.2.0" /> </dependentAssembly>
    <dependentAssembly> <assemblyIdentity name="System.Security.Cryptography.MACTripleDES"
    publicKeyToken="B03F5F7F11D50A3A" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.3.0.0" /> </dependentAssembly>
    <dependentAssembly> <assemblyIdentity name="System.Security.Cryptography.MACTripleDES" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.0.2.0" /> </dependentAssembly>
    <dependentAssembly> <assemblyIdentity name="System.Security.Cryptography.MACTripleDES"
    publicKeyToken="B03F5F7F11D50A3A" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.3.0.0" /> </dependentAssembly>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM