如何设置 IIS 用户在 IIS 上使用“System.Security.Cryptography.Csp”的权限?

问题描述

在 .net Core (.NET Core 5) Restfull API 项目上工作,并希望解密来自另一个应用程序的数据。

它在本地运行良好,但在 IIS(第 7 版)上运行不正常,似乎找不到文件“System.Security.Cryptography.Csp.dll”或无权访问它。

错误

Could not load file or assembly 'System.Security.Cryptography.Csp,Version=5.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a'. Reference assemblies should not be loaded for execution.
They can only be loaded in the Reflection-only loader context. (0x80131058)

代码

public static string Decrypt(string data)
    {
        string decryptedString=string.Empty;
        const int PROVIDER_RSA_FULL = 1;
        const string CONTAINER_NAME = "Tracker";

      
        CspParameters cspParams;
        cspParams = new CspParameters(PROVIDER_RSA_FULL);
        cspParams.KeyContainerName = CONTAINER_NAME;
       
        RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider(cspParams);
        rsa1.FromXmlString(privateKey);
        byte[] encyrptedBytes = Convert.FromBase64String(data);
        byte[] plain = rsa1.Decrypt(encyrptedBytes,RSAEncryptionPadding.Pkcs1);
        decryptedString = Encoding.UTF8.GetString(plain);

        return decryptedString;
    }

添加删除了这些行,但没有修复它:

   RSACryptoServiceProvider.UseMachineKeyStore = true;
  cspParams.Flags = CspProviderFlags.UseMachineKeyStore;

我发布了项目,将部署模式设置为自包含。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)