安装我的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尝试使用私钥时,它无法访问它。
查看证书,证书->所有任务->管理私钥。我可以看到我的用户不在列表中。通过GUI添加我的用户可以解决此问题,但我需要通过代码来完成!!