How to enable libsodium.net in ASP.NET

I have an old web service created on ASP.NET (using .asmx) files. I need to use sodium.net - unfortunately, it does not work when loading the dependent libsodium.dll file. Any ideas on what I'm doing wrong?

  • I added libsodium.net via NuGet.

  • I renamed the 64-bit DLL to "libsodium.dll" (and other naming conventions too).

  • I tried to reference libsodium.dll directly, but VS rejects it (not a valid DLL). So I added it as "content" instead of "copy to output".

  • After creating, I see that the / Bin website folder contains both sodium .dll (.NET assembly) and libsodium.net.

  • When I try to use libsodium.net, I get:

    ERROR 2015-02-02 11:14: 27,118 13798ms [41] CabinetService doRequest - Caught: the type initializer for "sodium sodium" left an exception. System.TypeInitializationException: The type initializer for "Sodium.SodiumCore" made an exception. ---> System.DllNotFoundException: Unable to load DLL 'libsodium.dll': the specified module was not found. (Exception from HRESULT: 0x8007007E) in DynamicDllInvokeType.sodium_init () on sodium. Sodium acid .cctor () --- End of internal check of exception stack --- on sodium .SodiumCore.LibraryName () on Sodium.SecretBox.Create (Byte [ ] message, Byte [] nonce, Byte []) in Macaroons.SecretBoxCryptoAlgorithm.Encrypt (Byte [] key, Byte [] plainText) in c: \ Projects \ Macaroons.Net \ Macaroons.Net \ SecretBoxCryptoAlgorithm.cs: line 58

Thus, he cannot find "libsodium.dll", even if it is located in the bin folder. I also tried to remove the dependency on "sodium .net", where after I got a runtime error saying that there was no "sodium .net" - when I added it again, this error disappeared and instead I got one above (indicating "sodium .net" loads correctly).

So, I open the shadow folder of the website in the folder "C: \ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ Temporary ASP.NET Files \ cabinetservice" and look for "sodium". The only result is "sodium .dll" in some subfolder. There is no "libsodium.dll".

So, ASP.NET ignores the file "libsodium.dll" when creating a shadow copy of the website.

I also tried adding libsodium.dll (32 bit) to C: \ Windows \ System32 and libsodium.dll (64 bit) to C: \ Windows \ SysWOW64. The same result.

And I tried C: \ Windows \ Microsoft.NET \ assembly \ GAC_32 \ libsodium and C: \ Windows \ Microsoft.NET \ assembly \ GAC_64 \ libsodium. The same result.

How can I make ASP.NET dependency information?

+7
c # dll libsodium
source share
2 answers

It turns out that ASP.NET does not create shadow copies of unmanaged DLLs such as libsodium.dll and libsodium-64.dll.

Sodium.dll (managed code) is trying to load DLLs from the same directory as the shadow copy of Sodium.dll (which is not going to work), or some of them in the PATH environment variable directories.

My solution was to add the AppDomain \ Bin directory to the path before calling any sodium code:

// Make it possible to load unmanaged libsodium DLLs that .NET does not make shadow copies of. // -> Simply point the "path" variable to the Bin directory. string path = Environment.GetEnvironmentVariable("PATH"); string binDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Bin"); Environment.SetEnvironmentVariable("PATH", path + ";" + binDir); 

January 2018 update (from Jordan Rieger's answer):

Please note that you may also need to install Microsoft Visual C ++ 2015 Redistributable on your server (either x64 or x86-target, depending on your process.)

Remember to restart the application pool (I did IIS reset) after installing the redistributable.

+16
source share

Note that you may also need to install the Microsoft Visual C ++ 2015 redistributable on your server (target x64 or x86, depending on your process.)

Remember to restart the application pool (I did IIS reset) after installing the redistributable.

See my comment on https://stackoverflow.com/a/4646268/2128 .

0
source share

All Articles