forked from stevenh/HttpServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecureHttpListener.cs
59 lines (53 loc) · 1.95 KB
/
SecureHttpListener.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Net;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
namespace HttpServer
{
/// <summary>
/// Secure version of the HTTP listener.
/// </summary>
public class SecureHttpListener : HttpListener
{
private readonly X509Certificate _certificate;
/// <summary>
/// Initializes a new instance of the <see cref="SecureHttpListener"/> class.
/// </summary>
/// <param name="address">Address to accept new connections on.</param>
/// <param name="port">Port to accept connections on.</param>
/// <param name="certificate">Certificate securing the connection.</param>
public SecureHttpListener(IPAddress address, int port, X509Certificate certificate) : base(address, port)
{
Protocol = SslProtocols.Tls;
_certificate = certificate;
}
/// <summary>
/// Gets if listener is secure.
/// </summary>
/// <value></value>
public override bool IsSecure
{
get { return true; }
}
/// <summary>
/// Gets or sets SSL protocol.
/// </summary>
public SslProtocols Protocol { get; set; }
/// <summary>
/// Gets or sets if client certificate should be used.
/// </summary>
public bool UseClientCertificate { get; set; }
/// <summary>
/// Create a new context
/// </summary>
/// <param name="socket">Accepted socket</param>
/// <returns>A new context.</returns>
/// <remarks>
/// Factory is assigned by the <see cref="HttpListener"/> on each incoming request.
/// </remarks>
protected override HttpContext CreateContext(Socket socket)
{
return Factory.Get<SecureHttpContext>(_certificate, Protocol, socket);
}
}
}