Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1596 was 1587, checked in by mbecirov, 15 years ago

#528 Adopt settings for activating/deactivating the use of certificates

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