当前位置: 首页 > 编程语言 > C#

如何设置.NETX.509证书私钥文件的读取权限Share

时间:2023-04-10 23:26:55 C#

如何设置.NETX.509证书私钥文件的读取权限下面是就是在Certstore代码中加入pfx。X509Storestore=newX509Store(StoreName.My,StoreLocation.LocalMachine);store.Open(OpenFlags.ReadWrite);X509Certificate2cert=newX509Certificate2("test.pfx","password");store.Add(证书);店铺。关闭();但是,我找不到一种方法来设置NetworkService访问私钥的权限。谁能解释一下?提前致谢。要以编程方式执行此操作,您必须做三件事:获取私钥文件夹的路径。获取此文件夹中私钥的文件名。为文件添加权限。请参阅本文(尤其是“AddAccessToCertificate”方法)以获取执行所有这三项操作的示例代码。这个答案已经很晚了,但我想我会把它发布给在这里搜索的其他人:我在这里找到了一篇使用CryptoKeySecurity的MSDN博客文章,这里有一个C#解决方案的例子:varrsa=certificate。作为RSACryptoServiceProvider的私钥;if(rsa!=null){//修改新CspParameters的CryptoKeySecurity然后实例化//一个新的RSACryptoServiceProvider似乎是保持访问规则的技巧。//比照。http://博客。msdn.com/b/cagatay/archive/2009/02/08/removing-acls-from-csp-key-containers.aspxvarcspParams=newCspParameters(rsa.CspKeyContainerInfo.ProviderType,rsa.CspKeyContainerInfo.ProviderName,rsa.CspKeyContainerInfo.KeyContainerName){Flags=CspProviderFlags.UseExistingKey|CspProviderFlags.UseMachineKeyStore,CryptoKeySecurity=rsa.CspKeyContainerInfo.CryptoKeySecurity};cspParams.CryptoKeySecurity.AddAccessRule(新的CryptoKeyAccessRule(sid,CryptoKeyRights.GenericRead,AccessControlType.Allow));using(varrsa2=newRSACryptoServiceProvider(cspParams)){//仅创建以保留规则change在CryptoKeySecurity}}我正在使用SecurityIdentifier来识别帐户,但NTAccount也可以正常工作如果这对其他任何人有帮助,我在Powershell写了JimFlood的答案functionSet-PrivateKeyPermissions{param([Parameter(Mandatory=$true)][string]$thumbprint,[Parameter(Mandatory=$false)][string]$account="NTAUTHORITYNETWORKSERVICE")#OpenCertificatestore并根据提供的指纹定位证书$store=New-ObjectSystem.Security.Cryptography.X509Certificates.X509Store("My","LocalMachine")$store.Open(“读写”)$cert=$store.Certificates|where{$_.Thumbprint-eq$thumbprint}#CreatenewCSPobjectbasedonexistingcertificateproviderandkeyname$csp=New-ObjectSystem.Security.Cryptography.CspParameters($cert.PrivateKey.CspKeyContainerInfo.ProviderType,$cert.PrivateKey.CspKeyContainerInfo.ProviderName,$cert.PrivateKey.CspKeyContainerInfo.KeyContainerName)#根据现有证书设置标志和密钥安全$csp.Flags="UseExistingKey","UseMachineKeyStore"$csp.CryptoKeySecurity=$cert.PrivateKey.CspKeyContainerInfo.CryptoKeySecurity$csp.KeyNumber=$cert.PrivateKey.CspKeyContainerInfo.KeyNumber#创建新的访问规则-可以使用权限参数,但我只需要GenericRead$access=New-ObjectSystem.Security.AccessControl.CryptoKeyAccessRule($account,"GenericRead","Allow")#向CSP对象添加访问规则$csp.CryptoKeySecurity.AddAccessRule($access)#CreatenewCryptoServiceProviderobjectwhichupdateKeywithCSPinformationcreated/modifiedabove$rsa2=New-ObjectSystem.Security.Cryptography.RSACryptoServiceProvider($csp)#Closecertificatestore$store.Close()}请注意,account参数也可以采用“DOMAINUSER”的形式(不仅仅是内置名称)——我在我的环境中对此进行了测试,它会自动转换这到相应的SID您可以使用作为WindowsServer2003资源工具包工具的一部分提供的WinHttpCertCfg.exe工具示例:winhttpcertcfg-g-cLOCAL_MACHINEMy-stest-aNetworkService或者,您可以使用“查找私钥工具”来找到证书私钥文件的磁盘位置。然后您只需使用ACL为文件设置正确的权限。示例:FindPrivateKeyMyLocalMachine-n??“CN=test”下面是我在WindowsServer2008上找到的解决方案,如果大家有兴趣的话:http://technet.microsoft.com/en-us/library/ee662329.aspx基本上,我必须使用MMC工具向需要访问证书的服务授予权限。奇迹般地工作。以上就是C#学习教程的全部内容:如何设置.NETX.509证书私钥文件的读取权限。整理自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: