以https HTTP侦听器支持C#编码 [英] http listener with https support coded in c#
问题描述
我找到了很多的答案如何设置的HttpListener为使用HTTPS的,但每一个解决方案需要使用命令行。我想这是做它的最快方法,但我想编写C#类来处理这个问题。
I found a lot of answers how to set httplistener up to use HTTPS, but each one solution requires the use of command line. I guess that is the fastest way to do it but I would like to write C# class to handle this.
在旧的解决方案,我用的Web服务器类(发现在互联网上的某个地方,我不记得确切的名称),它允许以这种方式添加证书:
In old solution I used webserver class (found somewhere on Internet, I don't remember exact name) which allowed to add certificate in that way:
webserver.Certificate = new X509Certificate2("MyCert.pfx", "MyPassword");
有没有办法用的HttpListener实现这一目标?从代码明显。
Is there way to achieve this with httplistener? From code obviously.
问候。
推荐答案
您可以加载与证书:
X509Certificate cert = new X509Certificate2("MyCert.pfx");
然后再进行安装:
And then install it:
X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
if (!store.Certificates.Contains(cert))
{
store.Add(cert);
}
store.Close();
当然,你可能需要更改商店名称或位置的特定的应用程序。
Of course you might have to change the store name or location for your particular app.
有关运行netsh命令,你可以看看创建和运行一个进程(即的的Process.Start )和netsh.exe中运行。否则,您必须与Win32混乱 HttpSetServiceConfiguration 。一>功能,或.NET等价的,如果有一个
For running the netsh command, you could look into creating and running a process (i.e. Process.Start) and run netsh.exe. Otherwise you have to mess with the Win32 HttpSetServiceConfiguration function, or the .NET equivalent if there is one.
您可能会发现这个codebox文章有用:的 http://dotnetcodebox.blogspot.com/2012/01/how-to-work-with-ssl- certificate.html
You might find this codebox article useful: http://dotnetcodebox.blogspot.com/2012/01/how-to-work-with-ssl-certificate.html
这篇关于以https HTTP侦听器支持C#编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!