如何设置 .NET 中 X.509 证书私钥文件的读取权限
以下是将 pfx 添加到证书存储的代码。
X509Store store = new X509Store( StoreName.My, StoreLocation.LocalMachine );
store.Open( OpenFlags.ReadWrite );
X509Certificate2 cert = new X509Certificate2( "test.pfx", "password" );
store.Add( cert );
store.Close();
但是,我找不到设置 NetworkService 访问私钥的权限的方法。
任何人都可以透露一些信息吗? 提前致谢。
Here is the code to add a pfx to the Cert store.
X509Store store = new X509Store( StoreName.My, StoreLocation.LocalMachine );
store.Open( OpenFlags.ReadWrite );
X509Certificate2 cert = new X509Certificate2( "test.pfx", "password" );
store.Add( cert );
store.Close();
However, I couldn't find a way to set permission for NetworkService to access the private key.
Can anyone shed some light? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这个答案迟到了,但我想将其发布给在这里搜索的其他人:
我找到了一篇 MSDN 博客文章,其中提供了使用 CryptoKeySecurity 的解决方案 此处,这是 C# 解决方案的示例:
我使用 SecurityIdentifier 来识别帐户但 NTAccount 也同样有效。
This answer is late but I wanted to post it for anybody else that comes searching in here:
I found an MSDN blog article that gave a solution using CryptoKeySecurity here, and here is an example of a solution in C#:
I'm using a SecurityIdentifier to identify the account but an NTAccount would work just as well.
如果这可以帮助其他人,我在 Powershell 中写了 Jim Flood 的答案
请注意,帐户参数也可以采用“DOMAIN\USER”的形式(不仅仅是内置名称) - 我在我的环境中测试了它,它会自动将其转换为适当的 SID
In case this helps anyone else out, I wrote Jim Flood's answer in Powershell
Note that the account parameter can be in the form of "DOMAIN\USER" as well (not just built in names) - I tested this in my environment and it automatically converted it to the appropriate SID
要以编程方式执行此操作,您必须执行三件事:
获取私钥文件夹的路径。
获取该文件夹中私钥的文件名。
添加对该文件的权限。
请参阅这篇文章了解一些示例代码所有三个(具体查看“AddAccessToCertificate”方法)。
To do it programmatically, you have to do three things:
Get the path of the private key folder.
Get the file name of the private key within that folder.
Add the permission to that file.
See this post for some example code that does all three (specifically look at the "AddAccessToCertificate" method).
您可以使用作为 < a href="http://www.microsoft.com/downloads/details.aspx?familyid=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en" rel="noreferrer">Windows Server 2003 资源工具包工具。
示例:
或者,您可以使用 WCF 附带的查找私钥工具 SDK,查找证书私钥文件在磁盘上的位置。 然后您可以简单地使用 ACL 来设置文件的正确权限。
示例:
You can use the WinHttpCertCfg.exe tool that ships as part of the Windows Server 2003 Resource Kit Tools.
Example:
Alternatively, you could use the Find Private Key tool that ships with the WCF SDK, to find the location on disk of the certificate's private key file. Then you can simply use ACL to set the right privileges on the file.
Example:
根据@russ的回答,
这个版本可以处理密钥存储提供程序和密钥存储提供程序。 传统加密服务提供商。
Based on @russ's answer,
This version copes with both Key Storage Provider & the Legacy Crypto Service Provider.
如果有人感兴趣的话,这是我为 Windows Server 2008 找到的解决方案: http:// technet.microsoft.com/en-us/library/ee662329.aspx
基本上,我必须向需要使用 MMC 工具访问证书的服务授予权限。 奇迹般有效。
This is the solution I found for windows server 2008 if anyone was interested: http://technet.microsoft.com/en-us/library/ee662329.aspx
Basically, I had to grant permissions to the service that needs to access the certificate using the MMC tool. Works like a charm.
如果
Certificate
的PrivateKey
是RSACng
类型,您可以走这条路线:对于LocalMachine:
如果它是用户证书,请使用
Environment .SpecialFolder.ApplicationData
并在文件夹roaming\microsoft\crypto\rsa
中查找sid
。If the
PrivateKey
ofCertificate
is of typeRSACng
you can go this route:For LocalMachine:
If it is a User Certificate use
Environment.SpecialFolder.ApplicationData
and find thesid
in the folderroaming\microsoft\crypto\rsa
instead.