Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Contracts/3.2/WcfSettings.cs @ 1640

Last change on this file since 1640 was 1640, checked in by kgrading, 15 years ago

vchanged max size (#467)

File size: 3.4 KB
Line 
1//#define USE_MSG_BINDING
2using System;
3using System.Collections.Generic;
4using System.Linq;
5using System.Text;
6using System.ServiceModel.Channels;
7using System.ServiceModel;
8using System.Security.Cryptography.X509Certificates;
9using System.Net;
10
11namespace 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      binding.MaxBufferSize = int.MaxValue;
27      binding.MaxReceivedMessageSize = int.MaxValue;
28      binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
29      binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
30#endif
31      return binding;
32    }
33
34    /// <summary>
35    /// Defines the used certificate for authentification located in a certification store.
36    /// </summary>
37    /// <param name="svchost">A service for which this certificate is applicable.</param>
38    public static void SetServiceCertificate(ServiceHost svchost) {
39#if USE_MSG_BINDING
40      svchost.Credentials.ServiceCertificate.SetCertificate(
41        StoreLocation.LocalMachine,
42        StoreName.My,
43        X509FindType.FindBySubjectName,
44        SERVERCERT);
45#endif
46    }
47
48    /// <summary>
49    /// Gets the currently active IP address.
50    /// <remarks>If more than one IP connections is active, the first one will be used.</remarks>
51    /// </summary>
52    /// <returns></returns>
53    public static string GetActiveIP() {
54      return System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections()[0].LocalEndPoint.Address.ToString();
55    }
56
57    /// <summary>
58    /// Gets the default port used for HIVE services.
59    /// </summary>
60    /// <returns></returns>
61    public static int GetDefaultPort() {
62      return DEFAULTPORT;
63    }
64  }
65
66  /// <summary>
67  /// This class verifies the certificate defined by <see cref="SetServerCertificate"></see> method. Normally,
68  /// the verification process is managed by the underlying operating system.
69  /// </summary>
70  /// <remarks>
71  /// WARNUNG: Dieser Code wird nur für Testzertifikate benötigt, wie sie beispielsweise von makecert erstellt werden.
72  /// Sie sollten diesen Code nicht in einer Produktionsumgebung verwenden.
73  /// </remarks>
74  public class PermissiveCertificatePolicy {
75    string subjectName;
76    static PermissiveCertificatePolicy currentPolicy;
77    PermissiveCertificatePolicy(string subjectName) {
78      this.subjectName = subjectName;
79      ServicePointManager.ServerCertificateValidationCallback +=
80          new System.Net.Security.RemoteCertificateValidationCallback(RemoteCertValidate);
81    }
82
83    public static void Enact(string subjectName) {
84      currentPolicy = new PermissiveCertificatePolicy(subjectName);
85    }
86
87    bool RemoteCertValidate(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error) {
88      if (cert.Subject == subjectName) {
89        return true;
90      }
91      return false;
92    }
93  }
94}
Note: See TracBrowser for help on using the repository browser.