如何使用httplistener启用https?

4
我正在尝试在Unity3D游戏中使用C#创建一个简单的https服务器,以通过Web浏览器访问。我已经使用openssl创建了服务器证书和密钥,但是我找不到一种跨平台的方式来将证书传递给服务器,而不需要在代码外进行任何其他配置。
我找到的大部分信息属于以下几类:
- 使用SslStream,但似乎只与TcpListener相关(我想要更高级别的东西,可以提供网页服务) - 需要外部仅限Windows的工具,如httpcfg,我不想使用 - 在证书存储中编程或手动安装证书,这似乎需要程序或用户拥有管理员/根权限
我知道在Python中你可以做这样的事情:
ssl.wrap_socket (httpd.socket, certfile='./server-crt.pem', keyfile='./server-key.pem', server_side=True)

...但是似乎在C#中并没有类似于httplistener的等效物,也没有system.security.securitymanager或其他任何东西。我假设/希望我只是漏掉了一些显而易见的东西。

值得一提的是,这是我目前为止所拥有的,只是将MSDN httplistener示例放入Unity脚本中:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Net;

public class SimpleListenerExample : MonoBehaviour {

    // This example requires the System and System.Net namespaces.
    public static void StartServer(string[] prefixes)
    {
        if (!HttpListener.IsSupported)
        {
            Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
            return;
        }
        // URI prefixes are required,
        // for example "http://contoso.com:8080/index/".
        if (prefixes == null || prefixes.Length == 0)
            throw new ArgumentException("prefixes");

        // Create a listener.
        HttpListener listener = new HttpListener();
        // Add the prefixes.
        foreach (string s in prefixes)
        {
            listener.Prefixes.Add(s);
        }

        /* and here's the part where I would load the server certificate ...somehow */

        listener.Start();
        Console.WriteLine("Listening...");
        // Note: The GetContext method blocks while waiting for a request. 
        HttpListenerContext context = listener.GetContext();
        HttpListenerRequest request = context.Request;
        // Obtain a response object.
        HttpListenerResponse response = context.Response;
        // Construct a response.
        string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
        byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
        // Get a response stream and write the response to it.
        response.ContentLength64 = buffer.Length;
        System.IO.Stream output = response.OutputStream;
        output.Write(buffer, 0, buffer.Length);
        // You must close the output stream.
        output.Close();
        listener.Stop();
    }

    // Use this for initialization
    void Start () {
        String[] prefixes = { "http://*:8089/", "https://*:8443/" };
        StartServer(prefixes);
    }

    // Update is called once per frame
    void Update () {

    }
}

1
上面的注释展示了如何在Windows上注册证书。如果你需要支持Mac或Linux,那么你必须将证书安装到Mono文件夹中。我在Jexus Manager Remote Services中有一些示例代码,https://github.com/jexuswebserver/jxmgr/blob/master/RemoteServices/Program.cs#L69 - Lex Li
@LexLi,你能写一篇关于如何安装这个的答案吗?因为这是Unity,我不认为Mac和Linux上有现成的解决方案。 - Programmer
非常感谢您的帮助。然而,这个问题链接所说的重复问题并没有被接受的答案,除了一个解决方案使用httpcfg和/或netsh之外。另一个答案使用了仅适用于Windows的API。我已经更改了我的问题标题,以澄清我真正寻找的是一个Mono解决方案。 - user2686455
我最近使用HTTPListener实现了HTTPS,并通过添加防火墙规则允许局域网通信。没有任何输入要求,一切都由C#代码处理。我认为这可以帮助你。我在这里分享了我的解决方案:https://stackoverflow.com/a/58149405/983548 - Habib Sheikh
1个回答

1

如果您从Google来到这里,试图找到有关Mono和SSL中HttpListener的信息,您将在以下相关问题中找到更相关的信息:

Mono HttpListener客户端证书

OP最初的愿望是一个简单的Web服务器,没有任何特定于平台的配置。到目前为止,我发现唯一支持避免平台配置方法的库是Ceen HTTPd。

关于Ceen的讨论来自一个有类似需求的发布者: https://softwarerecs.stackexchange.com/questions/52304/net-library-for-running-an-embedded-selfhosted-light-web-server-from-c-with-ss


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接