gpt4 book ai didi

c# - SSL 证书 : A specified logon session does not exist

转载 作者:太空宇宙 更新时间:2023-11-03 14:47:33 24 4
gpt4 key购买 nike

我已经创建了一个方法来创建一个证书,将它存储到证书存储并将它绑定(bind)到一个端口。这是方法:

private static void CreateStoreAndBindCertificate(string a_IpAddress, string a_IpPort)
{
Guid _AppId = Guid.Parse("b30f5be6-2920-4fa1-b0a6-5a56b63051bc");

var _RootCert = new RootCertificateContainer("CN=MyApp Root CA", 1024);
var _ServerCert = new ServerCertificateContainer("CN=MyAppApi", _RootCert, 1024);

//Here the Certificate will be created and then store
string _pathRootCertCER = Path.Combine(Path.GetTempPath(), "root-cert.cer");
string _pathServerCerPFX = Path.Combine(Path.GetTempPath(), "server-cert.pfx");

_RootCert.X509Certificate.PrivateKey = null;
File.WriteAllBytes(
_pathRootCertCER,
_RootCert.X509Certificate.Export(X509ContentType.Cert)
);

var _ServerCertPFX = new PFX(_ServerCert.X509Certificate);
File.WriteAllBytes(_pathServerCerPFX, _ServerCertPFX.GeneratePfxFile());

Process.Start(
new ProcessStartInfo()
{
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "certutil",
Arguments = string.Format("-f -p -importPFX \"{0}\"", _pathServerCerPFX)
}
).WaitForExit();

try
{
ICertificateBindingConfiguration config = new CertificateBindingConfiguration();
var _IpPort = new IPEndPoint(IPAddress.Parse(a_IpAddress), Convert.ToInt32(a_IpPort));
var certificateThumbprint = _ServerCert.X509Certificate.Thumbprint.ToLower();
if (config.Query(_IpPort).Length > 0)
config.Delete(_IpPort);
config.Bind(new CertificateBinding(certificateThumbprint, StoreName.My, _IpPort, _AppId));
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}

当我使用参数执行此方法时:CreateStoreAndBindCertificate("127.0.0.1", "9001"),我收到此错误:指定的登录 session 不存在。它可能已经终止。
我错过了什么?

最佳答案

要将证书绑定(bind)到我想要的端口,我需要使用密码创建 .pfx 证书。所以我会做同样的小改动:

const string passwordPFX = "MyPassword";

应添加在方法的开头。变化:

var _ServerCertPFX = new PFX(_serverCert.X509Certificate);

var _ServerCertPFX = new PFX(_serverCert.X509Certificate, passwordPFX);

改变:

Process.Start(
new ProcessStartInfo()
{
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "certutil",
Arguments = string.Format("-f -p -importPFX \"{0}\"", _pathServerCerPFX)
}
).WaitForExit();

到:

Process.Start(
new ProcessStartInfo()
{
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "certutil",
Arguments = string.Format("-f -p {0} -importPFX \"{1}\"", passwordPFX, _pathServerCerPFX)
}
).WaitForExit();

另一种可能的解决方案是您根本不使用密码,因此您必须从 ProcessStartInfo Arguments 中删除 -p . 如下所示:

Process.Start(
new ProcessStartInfo()
{
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "certutil",
Arguments = string.Format("-f -importPFX \"{0}\"", _pathServerCerPFX)
}
).WaitForExit();

关于c# - SSL 证书 : A specified logon session does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37724544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com