简体   繁体   English

自签名证书的 C# HttpListener (netsh) 问题

[英]C# HttpListener (netsh) problems with SelfSigned Certificate

I have some trouble with my HttpListener.我的 HttpListener 有一些问题。 I already searched and read some dicussions.我已经搜索并阅读了一些讨论。 For example: Httplistener with HTTPS support例如: 支持 HTTPS 的 Httplistener

Firstly I try to describe my scenario: I want to create a HTTP-Listener with SSL/HTTPS Support.首先,我尝试描述我的场景:我想创建一个支持 SSL/HTTPS 的 HTTP 侦听器。 Thats the main target.那是主要目标。 I used OpenSSL to create my own CA and I created my own server cert.我使用 OpenSSL 创建自己的 CA 并创建了自己的服务器证书。 Now I have:我现在有:

  • myCa.key我的密钥
  • myCa.pem myCapem
  • myCa.srl myCa.srl
  • myServer.key我的服务器密钥
  • myServer.csr我的服务器.csr
  • myServer.crt我的服务器.crt

I installed the myCa.pem and the myServer.crt certificate to my local computer.我将 myCa.pem 和 myServer.crt 证书安装到我的本地计算机。 I moved the CA in the trusted store and the server certificate in "own certificates"我将 CA 移动到受信任的存储中,并将服务器证书移动到“自己的证书”中

Then I took the fingerprint (certHash) of my server certificate.然后我获取了我的服务器证书的指纹(certHash)。 I created the netsh entry with admin-rights我使用管理员权限创建了 netsh 条目

netsh http add sslcert ipport=0.0.0.0:9649 appid= '{0a5ce-569a-4dc6-8ed7-9ef91241dec3}' certhash=4F556BDC3F97B31D555266DA74F573777FCCAA55

My C# implementation is relativly simple:我的 C# 实现相对简单:

    this.Listener = new HttpListener();
    this.Listener.Prefixes.Add("https://*:9649");
    this.Listener.Start();
    this.Listener.BeginGetContext(new AsyncCallback(ProcessClient), this.Listener);

   //Process an incoming connection
   private void ProcessClient(IAsyncResult result)
   {
      var listener = (HttpListener)result.AsyncState;
      var clientContext = listener.EndGetContext(result);
   }

When I implemented SSL in my TcpStack I used a SSL-Stream and there I can validate a certificate with a ValidationCallback.当我在我的 TcpStack 中实现 SSL 时,我使用了 SSL-Stream,在那里我可以使用 ValidationCallback 验证证书。 Im not sure if this is possible here.我不确定这是否可行。 I tried ServicePointManager.ServerCertificateValidationCallback += ValidateCert;我试过ServicePointManager.ServerCertificateValidationCallback += ValidateCert; But I never hit my breakpoint there.但我从来没有在那里达到我的断点。

Now to the problems:现在解决问题:

When I try to connect with my own HttpClient (.NET HttpClient Class) I get always a RemoteNameMismatch Error on the SSL-Layer.当我尝试连接我自己的 HttpClient(.NET HttpClient 类)时,SSL 层上总是出现RemoteNameMismatch错误。 I dont hit the breakpoint in the ProcessClient method.我没有命中ProcessClient方法中的断点。 I tried without specific certificate (auto detection) and I tried also to advise the same certificate (with the same hash) to the client.我尝试不使用特定证书(自动检测),并且还尝试向客户端建议相同的证书(具有相同的哈希)。 In both cases I got the same error.在这两种情况下,我都遇到了同样的错误。 I dont understand why I get any erros when I use the same certificate on the client and the server side.当我在客户端和服务器端使用相同的证书时,我不明白为什么会出现任何错误。 I always thought the netsh will compare the certhashes.我一直认为 netsh 会比较证书。

When I try a connect with Postman I hit the ProcessClient function.当我尝试与 Postman 连接时,我点击了ProcessClient function。 But Postman gets an error that he cant check the certificate.但是 Postman 收到一个错误,他无法检查证书。 But I think the problem is that my certificate isnt a official certifcate.但我认为问题在于我的证书不是官方证书。 But the data exchange is working.但是数据交换正在工作。

Another point is: I want to roll out my app also in containers with a unix os.另一点是:我还想在具有 unix 操作系统的容器中推出我的应用程序。 .NET60 is designed for crossplatform. .NET60专为跨平台而设计。 But whats the unix pendant to netsh?但是 netsh 的 unix 挂件是什么? Is it possible to run my listener with https on unix?是否可以在 unix 上使用 https 运行我的监听器? How works the mapping here between app and certificate?应用程序和证书之间的映射如何工作? Maybe I have to change my technology?也许我必须改变我的技术? Alternative to HttpListener ?替代HttpListener Mainly I dont want to use thridparty stuff.主要是我不想使用第三方的东西。

UPDATE Solution: Like the guys said in the in comments.更新解决方案:就像评论中的人所说的那样。 The FDQN was the problem. FDQN 是问题所在。 In easy words: I created my own CA and then I created a server cert signing request against the CA.简单来说:我创建了自己的 CA,然后针对 CA 创建了服务器证书签名请求。 Inside the server cert the CN is matching to my DNS of my personal computer.在服务器证书中,CN 与我的个人计算机的 DNS 匹配。 The connection with my HTTP-Listener is working now.与我的 HTTP 侦听器的连接现在正在工作。 Thank you for your help!谢谢您的帮助!

Thanks for reading and for help.感谢您的阅读和帮助。

Greetings问候

I use this post to mark the question as solved.我使用这篇文章将问题标记为已解决。 I dont find any other possibilites.我没有找到任何其他可能性。 The solution is in the updated question.解决方案在更新的问题中。

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

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