Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.OKB/3.3/CertificateServiceHost.cs @ 4279

Last change on this file since 4279 was 4279, checked in by swagner, 14 years ago

Integrated OKB services (#1166)

File size: 3.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.IO;
24using System.Reflection;
25using System.Security.Cryptography.X509Certificates;
26using System.ServiceModel;
27
28namespace HeuristicLab.Services.OKB {
29
30  /// <summary>
31  /// An alternate implementation of service host that checks client certificates.
32  /// </summary>
33  public class CertificateServiceHost : ServiceHost {
34
35    /// <summary>
36    /// Initializes a new instance of the <see cref="CertificateServiceHost"/> class.
37    /// </summary>
38    /// <param name="serviceType">Type of the service.</param>
39    public CertificateServiceHost(Type serviceType)
40      : base(serviceType) {
41    }
42
43    /// <summary>
44    /// Initializes a new instance of the <see cref="CertificateServiceHost"/> class.
45    /// </summary>
46    /// <param name="serviceType">Type of the service.</param>
47    /// <param name="baseAddresses">The base addresses.</param>
48    public CertificateServiceHost(Type serviceType, Uri[] baseAddresses)
49      : base(serviceType, baseAddresses) {
50    }
51
52    /// <summary>
53    /// Loads the service description information from the configuration file and applies it to the runtime being constructed.
54    /// </summary>
55    /// <exception cref="T:System.InvalidOperationException">The description of the service hosted is null.</exception>
56    protected override void ApplyConfiguration() {
57      base.ApplyConfiguration();
58      Credentials.ServiceCertificate.Certificate = GetCertificateResource("HeuristicLab.OKB.Server.server.pfx");
59      Credentials.ClientCertificate.Authentication.CertificateValidationMode =
60          System.ServiceModel.Security.X509CertificateValidationMode.Custom;
61      Credentials.ClientCertificate.Authentication.CustomCertificateValidator =
62        new CustomCertificateValidator(new[] { GetCertificateResource("HeuristicLab.OKB.Server.client.cer") });
63    }
64
65    private static X509Certificate2 GetCertificateResource(string name) {
66      using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name)) {
67        byte[] bytes;
68        bytes = new byte[(int)stream.Length];
69        stream.Read(bytes, 0, bytes.Length);
70        return new X509Certificate2(bytes);
71      }
72    }
73  }
74}
Note: See TracBrowser for help on using the repository browser.