1 | //#define USE_MSG_BINDING
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Text;
|
---|
6 | using System.ServiceModel.Channels;
|
---|
7 | using System.ServiceModel;
|
---|
8 | using System.Security.Cryptography.X509Certificates;
|
---|
9 | using System.Net;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Hive.Contracts {
|
---|
12 | public static class WcfSettings {
|
---|
13 |
|
---|
14 | public const string SERVERCERT = "HIVE-Server";
|
---|
15 | public const int DEFAULTPORT = 9000;
|
---|
16 |
|
---|
17 | /// <summary>
|
---|
18 | /// Gets a pre-defined binding using TCP for secure transport.
|
---|
19 | /// </summary>
|
---|
20 | /// <returns>A binding type of <see cref="NetTcpBinding"/></returns>
|
---|
21 | public static Binding GetBinding() {
|
---|
22 | #if USE_MSG_BINDING
|
---|
23 | NetTcpBinding binding = new NetTcpBinding(SecurityMode.Message);
|
---|
24 | #else
|
---|
25 | NetTcpBinding binding = new NetTcpBinding();
|
---|
26 | #endif
|
---|
27 | binding.MaxBufferSize = int.MaxValue;
|
---|
28 | binding.MaxReceivedMessageSize = int.MaxValue;
|
---|
29 | binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
|
---|
30 | binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
|
---|
31 | binding.CloseTimeout = new TimeSpan(0, 5, 0);
|
---|
32 | binding.ReceiveTimeout = new TimeSpan(0, 5, 0);
|
---|
33 | binding.SendTimeout = new TimeSpan(0, 5, 0);
|
---|
34 | return binding;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public static Binding GetStreamedBinding() {
|
---|
38 | #if USE_MSG_BINDING
|
---|
39 | NetTcpBinding binding = new NetTcpBinding(SecurityMode.Message);
|
---|
40 | #else
|
---|
41 | NetTcpBinding binding = new NetTcpBinding();
|
---|
42 | #endif
|
---|
43 | binding.TransferMode = TransferMode.Streamed;
|
---|
44 | binding.MaxReceivedMessageSize = int.MaxValue;
|
---|
45 | binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
|
---|
46 | binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
|
---|
47 | binding.CloseTimeout = new TimeSpan(0, 5, 0);
|
---|
48 | binding.ReceiveTimeout = new TimeSpan(0, 5, 0);
|
---|
49 | binding.SendTimeout = new TimeSpan(0, 5, 0);
|
---|
50 | //Disabling security mode, for the moment
|
---|
51 | binding.Security.Mode = SecurityMode.None;
|
---|
52 |
|
---|
53 | return binding;
|
---|
54 | }
|
---|
55 |
|
---|
56 | /// <summary>
|
---|
57 | /// Defines the used certificate for authentification located in a certification store.
|
---|
58 | /// </summary>
|
---|
59 | /// <param name="svchost">A service for which this certificate is applicable.</param>
|
---|
60 | public static void SetServiceCertificate(ServiceHost svchost) {
|
---|
61 | #if USE_MSG_BINDING
|
---|
62 | svchost.Credentials.ServiceCertificate.SetCertificate(
|
---|
63 | StoreLocation.LocalMachine,
|
---|
64 | StoreName.My,
|
---|
65 | X509FindType.FindBySubjectName,
|
---|
66 | SERVERCERT);
|
---|
67 | #endif
|
---|
68 | }
|
---|
69 |
|
---|
70 | /// <summary>
|
---|
71 | /// Gets the currently active IP address.
|
---|
72 | /// <remarks>If more than one IP connections is active, the first one will be used.</remarks>
|
---|
73 | /// </summary>
|
---|
74 | /// <returns></returns>
|
---|
75 | public static string GetActiveIP() {
|
---|
76 | return System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections()[0].LocalEndPoint.Address.ToString();
|
---|
77 | }
|
---|
78 |
|
---|
79 | /// <summary>
|
---|
80 | /// Gets the default port used for HIVE services.
|
---|
81 | /// </summary>
|
---|
82 | /// <returns></returns>
|
---|
83 | public static int GetDefaultPort() {
|
---|
84 | return DEFAULTPORT;
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | /// <summary>
|
---|
89 | /// This class verifies the certificate defined by <see cref="SetServerCertificate"></see> method. Normally,
|
---|
90 | /// the verification process is managed by the underlying operating system.
|
---|
91 | /// </summary>
|
---|
92 | /// <remarks>
|
---|
93 | /// WARNUNG: Dieser Code wird nur für Testzertifikate benötigt, wie sie beispielsweise von makecert erstellt werden.
|
---|
94 | /// Sie sollten diesen Code nicht in einer Produktionsumgebung verwenden.
|
---|
95 | /// </remarks>
|
---|
96 | /*
|
---|
97 | public class PermissiveCertificatePolicy {
|
---|
98 | string subjectName;
|
---|
99 | static PermissiveCertificatePolicy currentPolicy;
|
---|
100 | PermissiveCertificatePolicy(string subjectName) {
|
---|
101 | this.subjectName = subjectName;
|
---|
102 | ServicePointManager.ServerCertificateValidationCallback +=
|
---|
103 | new System.Net.Security.RemoteCertificateValidationCallback(RemoteCertValidate);
|
---|
104 | }
|
---|
105 |
|
---|
106 | public static void Enact(string subjectName) {
|
---|
107 | currentPolicy = new PermissiveCertificatePolicy(subjectName);
|
---|
108 | }
|
---|
109 |
|
---|
110 | bool RemoteCertValidate(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error) {
|
---|
111 | if (cert.Subject == subjectName) {
|
---|
112 | return true;
|
---|
113 | }
|
---|
114 | return false;
|
---|
115 | }
|
---|
116 | } */
|
---|
117 | }
|
---|