PageRenderTime 32ms CodeModel.GetById 17ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/WCFWebApi/src/Microsoft.ServiceModel.Internal/Microsoft/ServiceModel/HttpTransportSecurityExtensionMethods.cs

#
C# | 59 lines | 46 code | 10 blank | 3 comment | 11 complexity | fb521e68fe293730042442d63b0b8c80 MD5 | raw file
 1// <copyright>
 2//   Copyright (c) Microsoft Corporation.  All rights reserved.
 3// </copyright>
 4
 5namespace Microsoft.ServiceModel
 6{
 7    using System;
 8    using System.Net;
 9    using System.ServiceModel;
10    using System.ServiceModel.Channels;
11    using Microsoft.Server.Common;
12
13    internal static class HttpTransportSecurityExtensionMethods
14    {
15        internal static void ConfigureTransportProtectionAndAuthentication(this HttpTransportSecurity httpTransportSecurity, HttpsTransportBindingElement httpsTransportBindingElement)
16        {
17            Fx.Assert(httpTransportSecurity != null, "httpTransportSecurity cannot be null");
18            Fx.Assert(httpsTransportBindingElement != null, "httpsTransportBindingElement cannot be null");
19
20            httpTransportSecurity.ConfigureAuthentication(httpsTransportBindingElement);
21            httpsTransportBindingElement.RequireClientCertificate = httpTransportSecurity.ClientCredentialType == HttpClientCredentialType.Certificate;
22        }
23
24        internal static void ConfigureTransportAuthentication(this HttpTransportSecurity httpTransportSecurity, HttpTransportBindingElement httpTransportBindingElement)
25        {
26            Fx.Assert(httpTransportSecurity != null, "httpTransportSecurity cannot be null");
27            Fx.Assert(httpTransportBindingElement != null, "httpTransportBindingElement cannot be null");
28
29            if (httpTransportSecurity.ClientCredentialType == HttpClientCredentialType.Certificate)
30            {
31                throw Fx.Exception.AsError(new InvalidOperationException(SR.CertificateUnsupportedForHttpTransportCredentialOnly));
32            }
33
34            httpTransportSecurity.ConfigureAuthentication(httpTransportBindingElement);
35        }
36
37        internal static void DisableTransportAuthentication(this HttpTransportSecurity httpTransportSecurity, HttpTransportBindingElement httpTransportBindingElement)
38        {
39            Fx.Assert(httpTransportSecurity != null, "httpTransportSecurity cannot be null");
40            Fx.Assert(httpTransportBindingElement != null, "httpTransportBindingElement cannot be null");
41
42            httpTransportBindingElement.AuthenticationScheme = AuthenticationSchemes.Anonymous;
43            httpTransportBindingElement.ProxyAuthenticationScheme = AuthenticationSchemes.Anonymous;
44            httpTransportBindingElement.Realm = string.Empty;
45            httpTransportBindingElement.ExtendedProtectionPolicy = httpTransportSecurity.ExtendedProtectionPolicy;
46        }
47
48        private static void ConfigureAuthentication(this HttpTransportSecurity httpTransportSecurity, HttpTransportBindingElement httpTransportBindingElement)
49        {
50            Fx.Assert(httpTransportSecurity != null, "httpTransportSecurity cannot be null");
51            Fx.Assert(httpTransportBindingElement != null, "httpTransportBindingElement cannot be null");
52
53            httpTransportBindingElement.AuthenticationScheme = HttpClientCredentialTypeHelper.MapToAuthenticationScheme(httpTransportSecurity.ClientCredentialType);
54            httpTransportBindingElement.ProxyAuthenticationScheme = HttpProxyCredentialTypeHelper.MapToAuthenticationScheme(httpTransportSecurity.ProxyCredentialType);
55            httpTransportBindingElement.Realm = httpTransportSecurity.Realm;
56            httpTransportBindingElement.ExtendedProtectionPolicy = httpTransportSecurity.ExtendedProtectionPolicy;
57        }
58    }
59}