以编程方式将证书添加到个人存储库

4

我正在参与的项目包括一个MVC网站,该网站与WCF Web服务通信,并通过Windows身份验证进行身份验证。 我有一个用于身份委托的证书,现在我正在尝试以编程方式添加它。 要手动执行此操作,我需要在MMC中打开证书控件,将.pfx文件导入到个人证书中并输入密码。 然后,我必须单击“管理私钥”,并允许IIS_IUSRS获得权限。 为了复制此过程,我编写了以下控制台应用程序:

class Program
{
    static void Main(string[] args)
    {
        var cert = new X509Certificate2("location.pfx", "password", X509KeyStorageFlags.MachineKeySet);
        AddCert(StoreName.My, StoreLocation.LocalMachine, cert);
        AddAccessToCertificate(cert, "IIS_IUSRS");
    }

    private static void AddCert(StoreName storeName, StoreLocation storeLocation, X509Certificate2 cert)
    {
        X509Store store = new X509Store(storeName, storeLocation);
        store.Open(OpenFlags.ReadWrite);
        store.Add(cert);
        store.Close();
    }

    private static void AddAccessToCertificate(X509Certificate2 cert, string user)
    {
        RSACryptoServiceProvider rsa = cert.PrivateKey as RSACryptoServiceProvider;

        if (rsa != null)
        {
            string keyfilepath =
                FindKeyLocation(rsa.CspKeyContainerInfo.UniqueKeyContainerName);

            FileInfo file = new FileInfo(keyfilepath + "\\" +
                rsa.CspKeyContainerInfo.UniqueKeyContainerName);

            FileSecurity fs = file.GetAccessControl();

            NTAccount account = new NTAccount(user);
            fs.AddAccessRule(new FileSystemAccessRule(account,
            FileSystemRights.FullControl, AccessControlType.Allow));

            file.SetAccessControl(fs);
        }
    }
    private static string FindKeyLocation(string keyFileName)
    {
        string text1 =
        Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
        string text2 = text1 + @"\Microsoft\Crypto\RSA\MachineKeys";
        string[] textArray1 = Directory.GetFiles(text2, keyFileName);
        if (textArray1.Length > 0)
        {
            return text2;
        }
        string text3 =
        Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        string text4 = text3 + @"\Microsoft\Crypto\RSA\";
        textArray1 = Directory.GetDirectories(text4);
        if (textArray1.Length > 0)
        {
            foreach (string text5 in textArray1)
            {
                textArray1 = Directory.GetFiles(text5, keyFileName);
                if (textArray1.Length != 0)
                {
                    return text5;
                }
            }
        }
        return "Private key exists but is not accessible";
    }
}

不幸的是,这会导致错误:

安全令牌发行者的地址未指定。必须在目标“https://service.svc”的绑定中显式指定发行者地址,或在凭据中配置本地发行者地址。

我意识到我对这些东西有很大的知识空白,所以我希望得到一些指导!

我的问题是,我的手动和自动化过程之间有什么区别?

1个回答

4

这一行代码:

var cert = new X509Certificate2("location.pfx", "password", X509KeyStorageFlags.MachineKeySet);

应该改成:

var cert = new X509Certificate2("location.pfx", "password", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);

缺少的是 X509KeyStorageFlags.PersistKeySet

我从这里获取了一些有关证书的有用信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接