RSACryptoServiceProvider CryptographicException 系统找不到 ASP.NE

RSACryptoServiceProvider CryptographicException System Cannot Find the File Specified under ASP.NET(RSACryptoServiceProvider CryptographicException 系统找不到 ASP.NET 下指定的文件)
本文介绍了RSACryptoServiceProvider CryptographicException 系统找不到 ASP.NET 下指定的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个使用 RSACryptoServiceProvider 使用已知私钥(存储在变量中)解密某些数据的应用程序.

I have an application which is making use of the RSACryptoServiceProvider to decrypt some data using a known private key (stored in a variable).

当 IIS 应用程序池配置为使用网络服务时,一切运行正常.

When the IIS Application Pool is configured to use Network Service, everything runs fine.

但是,当我们配置 IIS 应用程序池以在不同的身份下运行代码时,我们会得到以下信息:

However, when we configure the IIS Application Pool to run the code under a different Identity, we get the following:

System.Security.Cryptography.CryptographicException: The system cannot find the file specified.

   at System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer)
   at System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(RSAParameters parameters)
   at System.Security.Cryptography.RSA.FromXmlString(String xmlString)

代码是这样的:

byte[] input; 
byte[] output; 
string private_key_xml; 

var provider = new System.Cryptography.RSACryptoServiceProvider(this.m_key.Key_Size);
provider.FromXmlString(private_key_xml); // Fails Here when Application Pool Identity != Network Service

ouput = provider.Decrypt(input, false); // False = Use PKCS#1 v1.5 Padding

有一些资源试图通过说明您应该授予用户对机器密钥存储的读取权限来回答这个问题 - 但是没有明确的答案来解决这个问题.

There are resources which attempt to answer it by stating that you should give the user read access to the machine key store - however there is no definitive answer to solve this issue.

环境:IIS 6.0、Windows Server 2003 R2、.NET 3.5 SP1

Environment: IIS 6.0, Windows Server 2003 R2, .NET 3.5 SP1

推荐答案

确实需要这样的代码

CspParameters _cpsParameter;
RSACryptoServiceProvider RSAProvider;

_cpsParameter = new CspParameters();
_cpsParameter.Flags = CspProviderFlags.UseMachineKeyStore;

RSAProvider = new RSACryptoServiceProvider(1024, _cpsParameter); 

以下用户需要访问该文件夹:C:Documents and SettingsAll UsersApplication dataMicrosoftCryptoRSAMachineKeys

The following users need access to the folder: C:Documents and SettingsAll UsersApplication dataMicrosoftCryptoRSAMachineKeys

  1. IIS 用户帐户(匿名)
  2. 您在 web.config 设置中用于模拟您的应用程序的用户帐户.

所以现在它对我来说很好.

So now it is working fine for me.

这篇关于RSACryptoServiceProvider CryptographicException 系统找不到 ASP.NET 下指定的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Custom Error Queue Name when using EasyNetQ for RabbitMQ?(使用 EasyNetQ for RabbitMQ 时自定义错误队列名称?)
Rabbitmq Ack or Nack, leaving messages on the queue(Rabbitmq Ack 或 Nack,将消息留在队列中)
Wait for a single RabbitMQ message with a timeout(等待一条带有超时的 RabbitMQ 消息)
Setup RabbitMQ consumer in ASP.NET Core application(在 ASP.NET Core 应用程序中设置 RabbitMQ 消费者)
How do I set a number of retry attempts in RabbitMQ?(如何在 RabbitMQ 中设置重试次数?)
WebClient set headers(WebClient 设置标头)