How to set permission for IIS User to use "System.Security.Cryptography.Csp" on the IIS?

0

Working on a .net core (.NET Core 5) Restfull API project and want to Decrypt data that comes from another application.

It works fine on the local but not working on the IIS(ver 7) and it seems it can not find file "System.Security.Cryptography.Csp.dll" or has not permission to access it.

Error:

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)

Codes:

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;
    }

Added or removed these lines but it doesn't fix it:

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

I published project with set Deployment mode as Selef-Contained.

c#
.net-core
rsacryptoserviceprovider
asked on Stack Overflow Jan 13, 2021 by motevalizadeh

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0