【问题标题】:http listener with https support coded in c#带有 https 支持的 http 侦听器,用 c# 编码
【发布时间】:2014-03-04 23:05:22
【问题描述】:

我找到了很多关于如何将 httplistener 设置为使用 HTTPS 的答案,但是每个解决方案都需要使用命令行。我想这是最快的方法,但我想编写 C# 类来处理这个问题。

在旧的解决方案中,我使用了允许以这种方式添加证书的 webserver 类(在 Internet 上的某个地方找到,我不记得确切的名称):

webserver.Certificate = new X509Certificate2("MyCert.pfx", "MyPassword");

有没有办法通过 httplistener 实现这一点?显然来自代码。

问候。

【问题讨论】:

  • 你想在 C# 中复制什么命令行命令?似乎您应该首先让您的 http 侦听器工作,使用这种方式您将确切地知道您需要在 C# 代码中复制哪些设置命令。
  • 我的 httplistener 运行良好,但我的雇主坚持使用加密连接。这个解决方案 (stackoverflow.com/questions/11403333/…) 包含我需要做的所有事情。

标签: c# https certificate httplistener


【解决方案1】:

您可以通过以下方式加载证书:

X509Certificate cert = new X509Certificate2("MyCert.pfx");

然后安装:

X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
if (!store.Certificates.Contains(cert))
{
    store.Add(cert);
}
store.Close();

当然,您可能需要更改特定应用的商店名称或位置。

要运行 netsh 命令,您可以查看创建和运行进程(即Process.Start)并运行 netsh.exe。否则你必须弄乱 Win32 HttpSetServiceConfiguration 函数,或者 .NET 等效函数(如果有的话)。

您可能会发现这篇代码框文章很有用:http://dotnetcodebox.blogspot.com/2012/01/how-to-work-with-ssl-certificate.html

【讨论】:

猜你喜欢
  • 2015-03-24
  • 2019-02-04
  • 1970-01-01
  • 2014-05-21
  • 1970-01-01
  • 2018-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多