10

我将源代码从一个应用程序复制到另一个应用程序,两者都在同一台机器上运行。我还在两个应用程序中为下面的 containerName 使用相同的字符串。

是什么阻止我的新应用程序读取保存在其他应用程序中的密钥?所有其他事情都是平等的,登录用户帐户等。

     CspParameters cspParams = new CspParameters();
     cspParams.KeyContainerName = containerName;
     cspParams.Flags = CspProviderFlags.UseMachineKeyStore;

     // Get error "object already exists" below.
     RSACryptoServiceProvider  rsaKey = new RSACryptoServiceProvider(cspParams);
4

4 回答 4

7

您是否尝试向所有人授予权限,例如,对于“Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\Machine Keys”中的文件,如其所述:

http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/f7b9f928-a794-47f2-a5bd-9f64ca375040

于 2011-01-21T22:07:09.470 回答
6

另一种解决方案是通过代码设置每个人的访问权限:

CspParameters cspParams;
cspParams = new CspParameters(PROVIDER_RSA_FULL);
cspParams.KeyContainerName = CONTAINER_NAME;
cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";

CryptoKeyAccessRule rule = new CryptoKeyAccessRule("everyone", CryptoKeyRights.FullControl, AccessControlType.Allow);

cspParams.CryptoKeySecurity = new CryptoKeySecurity();
cspParams.CryptoKeySecurity.SetAccessRule(rule);
于 2011-09-02T12:38:55.080 回答
2

我遇到了这个问题,因为我的 WCF 服务没有访问密钥库的权限。我按照在此处找到的授予用户 ASPNET 读取访问权限的说明解决了这个问题:http: //msdn.microsoft.com/en-us/library/2w117ede.aspx#Y898

于 2011-03-01T14:21:34.177 回答
1

我最近在单个服务器(Windows 2008 R2)上部署了多个 IIS 站点时遇到了这个问题。我们的环境中的每个站点都在不同的应用程序池上运行,但在某些情况下,可以为这些池分配相同的标识。

如果密钥不存在,我们的应用程序会创建一个密钥,并将其放置在一个容器中,该容器的名称基于当前身份。第一个部署的站点始终有效,但如果我们将另一个站点部署到具有相同标识的另一个应用程序池中,第二个站点就会失败。

事实证明,当存储密钥时,Windows 会授予用户“IIS APPPOOL\AppPoolName”的完全访问权限,而不是我们分配给池的身份。

因此,我们的解决方案是为容器提供对当前身份的显式权限(这类似于@Webmixer 的答案,唯一的区别在于CryptoKeyAccessRule):

CspParameters cspParams;
cspParams = new CspParameters(PROVIDER_RSA_FULL);
cspParams.KeyContainerName = CONTAINER_NAME;
cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";

CryptoKeyAccessRule rule = new CryptoKeyAccessRule(System.Security.Principal.WindowsIdentity.GetCurrent(), CryptoKeyRights.FullControl, AccessControlType.Allow);

cspParams.CryptoKeySecurity = new CryptoKeySecurity();
cspParams.CryptoKeySecurity.SetAccessRule(rule);
于 2014-09-09T16:58:59.440 回答