c# - Libsodium-net - 无法加载 DLL 'libsodium.dll

标签 c# .net dll asp.net-web-api2 libsodium

我通过 NuGet 安装了 Libsodium-net 并且能够在我的类中包含 Sodium,但是当我尝试运行它时,我得到了

Sodium.dll 中发生了“System.DllNotFoundException”类型的异常,但未在用户代码中处理

附加信息:无法加载 DLL“libsodium.dll”:找不到指定的模块。 (HRESULT 异常:0x8007007E)

我只是想运行 gitbooks 文档中的示例代码 https://bitbeans.gitbooks.io/libsodium-net/content/password_hashing/index.html

const string PASSWORD = "Correct Horse Battery Staple";
const string SALT = "qa~t](84z<1t<1oz:ik.@IRNyhG=8q(o";
const long OUTPUT_LENGTH = 512;

//this will produce a 512 byte hash
var hash = PasswordHash.ScryptHashBinary(PASSWORD, SALT,      PasswordHash.Strength.Medium, OUTPUT_LENGTH);

最佳答案

我遇到了同样的问题并使用 Jørn Wildt's answer given here 解决了它.

It turns out that ASP.NET doesn't make shadow copies of unmanaged DLLs such as libsodium.dll and libsodium-64.dll.

Sodium.dll (the managed code) tries to load the DLLs from either the same directory as the shadow copy of Sodium.dll (which is not going to work) - or some where in the PATH environment variable's directories.

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

string path = Environment.GetEnvironmentVariable("PATH");
string binDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Bin");
Environment.SetEnvironmentVariable("PATH", path + ";" + binDir);

正如鲁本对答案的评论;我在 Global.asaxApplication_Start 方法中添加了上述代码。

关于c# - Libsodium-net - 无法加载 DLL 'libsodium.dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37347620/

相关文章:

c# - Entity Framework 6 连接字符串 dll 应用程序配置

C#.NET & 外部组件的翻译

c# - 奇怪的空字符串连接行为

c# - .Net TableLayoutPanel – 清除控件非常慢

dll - 如何查明包是否允许重新分发程序集?

c++ - Visual Studio 中的静态库与 DLL

javascript - GET 请求调用返回错误结果

c# - 如何使 ValidatesOnDataErrors 可绑定(bind)?

c# - 将附件添加到 MailMessage 时出现流关闭错误

c# - 将文件扩展名关联到 C# 应用程序中的应用程序