如何使用c#以编程方式将证书安装到本地计算机存储中?

J D*_*vis 53 .net c# wcf certificate makecert

我有通过MakeCert生成的证书.我想使用PeerTrust将此证书用于WCF消息安全性.如何使用c#或.NET以编程方式将证书安装到"受信任的人"本地计算机证书存储中?

我有一个CER文件,但也可以创建一个PFX.

Dem*_*emi 59

我相信这是正确的:

using (X509Store store = new X509Store(StoreName.TrustedPeople, StoreLocation.LocalMachine)) 
{
   store.Open(OpenFlags.ReadWrite);
   store.Add(cert); //where cert is an X509Certificate object
}
Run Code Online (Sandbox Code Playgroud)

  • 我猜这个答案现在无效,因为 X509Store 不是一次性的。 (2认同)

小智 40

以下对我有用:

private static void InstallCertificate(string cerFileName)
{
    X509Certificate2 certificate = new X509Certificate2(cerFileName);
    X509Store store = new X509Store(StoreName.TrustedPublisher, StoreLocation.LocalMachine);

    store.Open(OpenFlags.ReadWrite);
    store.Add(certificate);
    store.Close();
}
Run Code Online (Sandbox Code Playgroud)

  • 要在“Personnel”节点中存储证书,请选择枚举值“StorName.My”。要在“受信任的根证书颁发机构”节点中存储证书,请选择枚举值“StorName.Root”。 (4认同)

小智 7

而不是将证书安装到需要提升权限的LocalMachine,您可以将其添加到"CurrentUser"(适合我).

X509Store store = new X509Store(StoreName.TrustedPeople, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
store.Add(cert); //where cert is an X509Certificate object
store.Close();
Run Code Online (Sandbox Code Playgroud)