我想在我的.NET Standard 1.4库中使用System.Security.Cryptography.RNGCryptoServiceProvider类,根据这个话题,我的代码看起来像这样:
private byte[] GenerateRandomNumber(int length)
{
using (var randomNumberGenerator = RandomNumberGenerator.Create())
{
var number = new byte[length];
randomNumberGenerator.GetBytes(number);
return number;
}
}
我还从NuGet安装了以下库:
- System.Security.Cryptography.Algorithms v=4.3.0
- System.Security.Cryptography.Primitives v=4.3.0
但是尝试启动它会给我以下错误信息:
'Could not load file or package' System.Security.Cryptography.Algorithms, Version = 4.1.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a 'or one of its dependencies. The specified file could not be found. '
NuGet页上没有4.1.0.0版本,只有4.1.0-rc2-24027版本。安装这个版本后我得到完全相同的异常。出了什么问题?
编辑:从.NET Standard 1.4切换到1.6也没有帮助。
编辑2:
当我在RandomNumberGenerator
上按F12时:#region Assembly System.Security.Cryptography.Algorithms, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// C:\Users\x.y\.nuget\packages\system.security.cryptography.algorithms\4.3.0\ref\netstandard1.4\System.Security.Cryptography.Algorithms.dll
#endregion
namespace System.Security.Cryptography
{
public abstract class RandomNumberGenerator : IDisposable
{
protected RandomNumberGenerator();
public static RandomNumberGenerator Create();
public void Dispose();
public abstract void GetBytes(byte[] data);
protected virtual void Dispose(bool disposing);
}
}
它需要4.1.0版本(但NuGet上不存在),但路径被设置为4.3.0
System.IO (>= 4.3.0)
、System.Runtime (>= 4.3.0)
、System.Security.Cryptography.Primitives (>= 4.3.0)
- Equalskapp.config
中添加一个绑定重定向,将所需版本重定向到实际版本。 - Martin Liversage