2 // BasicHttpsBinding.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
6 // Martin Baulig <martin.baulig@xamarin.com>
8 // Copyright (C) 2005-2006 Novell, Inc. http://www.novell.com
9 // Copyright 2011-2012 Xamarin Inc (http://www.xamarin.com).
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections.Generic;
33 using System.Net.Security;
34 using System.ServiceModel.Channels;
35 using System.ServiceModel.Description;
38 using System.ServiceModel.Configuration;
40 namespace System.ServiceModel
42 public class BasicHttpsBinding : HttpBindingBase,
43 IBindingRuntimePreferences
45 WSMessageEncoding message_encoding = WSMessageEncoding.Text;
46 BasicHttpsSecurity security;
48 public BasicHttpsBinding ()
49 : this (BasicHttpsSecurityMode.Transport)
53 #if !NET_2_1 && !XAMMAC_4_5
54 public BasicHttpsBinding (string configurationName)
57 BindingsSection bindingsSection = ConfigUtil.BindingsSection;
58 BasicHttpsBindingElement el =
59 bindingsSection.BasicHttpsBinding.Bindings [configurationName];
61 el.ApplyConfiguration (this);
65 public BasicHttpsBinding (
66 BasicHttpsSecurityMode securityMode)
68 security = new BasicHttpsSecurity (securityMode);
71 public WSMessageEncoding MessageEncoding {
72 get { return message_encoding; }
73 set { message_encoding = value; }
76 public override string Scheme {
77 get { return Uri.UriSchemeHttps; }
80 public BasicHttpsSecurity Security {
81 get { return security; }
84 public override BindingElementCollection
85 CreateBindingElements ()
87 var list = new List<BindingElement> ();
89 var security = CreateSecurityBindingElement ();
93 list.Add (BuildMessageEncodingBindingElement ());
94 list.Add (GetTransport ());
96 return new BindingElementCollection (list.ToArray ());
99 SecurityBindingElement CreateSecurityBindingElement ()
101 SecurityBindingElement element;
102 switch (Security.Mode) {
103 case BasicHttpsSecurityMode.TransportWithMessageCredential:
104 #if NET_2_1 || XAMMAC_4_5
106 throw new NotImplementedException ();
108 if (Security.Message.ClientCredentialType != BasicHttpMessageCredentialType.Certificate)
109 // FIXME: pass proper security token parameters.
110 element = SecurityBindingElement.CreateCertificateOverTransportBindingElement ();
112 element = new AsymmetricSecurityBindingElement ();
119 #if !NET_2_1 && !XAMMAC_4_5
120 element.SetKeyDerivation (false);
121 element.SecurityHeaderLayout = SecurityHeaderLayout.Lax;
126 MessageEncodingBindingElement BuildMessageEncodingBindingElement ()
128 if (MessageEncoding == WSMessageEncoding.Text) {
129 TextMessageEncodingBindingElement tm = new TextMessageEncodingBindingElement (
130 MessageVersion.CreateVersion (EnvelopeVersion, AddressingVersion.None), TextEncoding);
131 ReaderQuotas.CopyTo (tm.ReaderQuotas);
134 #if NET_2_1 || XAMMAC_4_5
135 throw new NotImplementedException ();
137 return new MtomMessageEncodingBindingElement (
138 MessageVersion.CreateVersion (EnvelopeVersion, AddressingVersion.None), TextEncoding);
143 TransportBindingElement GetTransport ()
145 HttpsTransportBindingElement h = new HttpsTransportBindingElement ();
147 h.AllowCookies = AllowCookies;
148 h.BypassProxyOnLocal = BypassProxyOnLocal;
149 h.HostNameComparisonMode = HostNameComparisonMode;
150 h.MaxBufferPoolSize = MaxBufferPoolSize;
151 h.MaxBufferSize = MaxBufferSize;
152 h.MaxReceivedMessageSize = MaxReceivedMessageSize;
153 h.ProxyAddress = ProxyAddress;
154 h.UseDefaultWebProxy = UseDefaultWebProxy;
155 h.TransferMode = TransferMode;
157 h.ExtendedProtectionPolicy = Security.Transport.ExtendedProtectionPolicy;
159 switch (Security.Transport.ClientCredentialType) {
160 case HttpClientCredentialType.Basic:
161 h.AuthenticationScheme = AuthenticationSchemes.Basic;
163 case HttpClientCredentialType.Ntlm:
164 h.AuthenticationScheme = AuthenticationSchemes.Ntlm;
166 case HttpClientCredentialType.Windows:
167 h.AuthenticationScheme = AuthenticationSchemes.Negotiate;
169 case HttpClientCredentialType.Digest:
170 h.AuthenticationScheme = AuthenticationSchemes.Digest;
172 case HttpClientCredentialType.Certificate:
173 h.RequireClientCertificate = true;