简体   繁体   English

c#X509Certificate2添加证书如何标记为可导出

[英]c# X509Certificate2 add certificate how mark as exportable

I have a .NET 4.0 program which is running localhost on port 9000. I want to support SSL and have a .pfx certificate to import. 我有一个.NET 4.0程序在端口9000上运行localhost。我想支持SSL并且要导入.pfx证书。

Because the program is running at multiple computers the programm itself is responsible to store the certificate and register the its port. 由于程序在多台计算机上运行,​​因此程序本身负责存储证书并注册其端口。

When imported and registered by the program, everything works. 当程序导入和注册时,一切正常。 But when I reboot the computer the https connection does not work any.. more.. 但是,当我重新启动计算机时,https连接不起作用..更多..

The event viewer gives the following error: 事件查看器给出以下错误:

A fatal error occurred when attempting to access the SSL server credential private key. 尝试访问SSL服务器凭据私钥时发生致命错误。 The error code returned from the cryptographic module is 0x8009030D. 加密模块返回的错误代码是0x8009030D。 The internal error state is 10001. 内部错误状态为10001。

and

An error occurred while using SSL configuration for endpoint 0.0.0.0:9000. 使用端点0.0.0.0:9000的SSL配置时发生错误。 The error status code is contained within the returned data. 错误状态代码包含在返回的数据中。

I found some solutions which state that you need to mark the certificate as 'exportable' while importing but I can't find how I do that in code. 我找到了一些解决方案,说明您需要在导入时将证书标记为“可导出”,但我无法在代码中找到我是如何做到这一点的。

Website 1 and Website 2 网站1网站2

Storing the certificate: 存储证书:

String path = String.Concat(IOHelper.AssemblyPath, @"\certificate.pfx");
X509Certificate2 cert = new X509Certificate2(path, "password");

X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);

if(!store.Certificates.Contains(cert))
{
    store.Add(cert);
}

Registering the host: 注册主机:

netsh http add sslcert ipport=0.0.0.0:9000 certhash=<cert hash> appid=<app id>

To mark a X509Certificate2 as exportable is actually quite simple. X509Certificate2标记为可导出实际上非常简单。 You add a third parameter ( X509KeyStorageFlags.Exportable ) which indicates that the certificate is exportable. 您添加第三个参数( X509KeyStorageFlags.Exportable ),表示证书是可导出的。

var cert = new X509Certificate2(path, "password", X509KeyStorageFlags.Exportable);

Further to Madeillei's answer, you can pass the following flags: 继Madeillei的回答,您可以传递以下标志:

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

If you are storing the cert in the CurrentUser store rather than in the LocalMachine store, do the following: 如果要将证书存储在CurrentUser存储中而不是存储在LocalMachine存储中,请执行以下操作:

var cert = new X509Certificate2(path, "password", X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable| X509KeyStorageFlags.UserKeySet);

The Key set flags indicate the following: 键集标志表示以下内容:

    //
    // Summary:
    //     Private keys are stored in the current user store rather than the local computer
    //     store. This occurs even if the certificate specifies that the keys should go
    //     in the local computer store.
    UserKeySet = 1,
    //
    // Summary:
    //     Private keys are stored in the local computer store rather than the current user
    //     store.
    MachineKeySet = 2,

The private key needs to be in the same place as the rest of the certificate for it to work. 私钥需要与证书的其余部分位于同一位置才能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM