首页 > 解决方案 > 授予用户对私钥的权限

问题描述

安装我的 WCF 服务我还安装了带有私钥的证书。由于我将以其他用户身份运行服务,因此该用户需要访问私钥。我广泛阅读了其他stackoverflow问题,他们都建议需要调整文件系统中私钥文件的权限。我这样做,

        private static void AddUserPermissions(X509Certificate2 certificate, NTAccount user, StoreLocation storeLocation)
        {
            RSACryptoServiceProvider rsaProvider = (RSACryptoServiceProvider)certificate.PrivateKey;

            // Find file 
            string keyPath = FindKeyLocation(rsaProvider.CspKeyContainerInfo.UniqueKeyContainerName, storeLocation);
            FileInfo keyFileInfo = new FileInfo(keyPath);

            // Create new FileSecurity
            FileSecurity keyFileSecurity = keyFileInfo.GetAccessControl();
            keyFileSecurity.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.Read, AccessControlType.Allow));

            // Apply file security to the file
            keyFileInfo.SetAccessControl(keyFileSecurity);
        }

当我运行我的程序并检查我可以看到的私钥文件时,例如“网络服务”已添加到权限列表中。

很好,这很有效,但是当 WCF 尝试使用私钥时,它无法访问它。

查看 certlm,certificate -> All Tasks -> Manage Private Keys.. 我可以看到我的用户不在列表中。通过 GUI 添加我的用户解决了这个问题,但我需要在代码中完成!

标签: c#windowsfilecertificatex509certificate

解决方案


加密服务提供程序(Microsoft RSA SChannel 加密提供程序)

密钥位于,C:\ProgramData\Application Data\Microsoft\Crypto\RSA\MachineKeys此处设置正常文件权限反映在certlm.msc.

CSP

下一代加密(Microsoft 密钥存储提供程序)

密钥位于,C:\ProgramData\Application Data\Microsoft\Crypto\Keys此处设置正常文件权限反映在certlm.msc.

天然气

概括

确保在正确的位置修改正确文件的权限。


推荐阅读